2
[sagar@BL-53 RcTools]$ irb
1.9.3p0 :001 > require 'csv'
 => true
1.9.3p0 :002 > master = CSV.read("./public/jobs/in/Appexchange_Applications_Companies_487.csv")
ArgumentError: invalid byte sequence in UTF-8
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1855:in `sub!'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1855:in `block in shift'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1849:in `loop'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1849:in `shift'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1791:in `each'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1805:in `to_a'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1805:in `read'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1411:in `block in read'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1354:in `open'
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/csv.rb:1411:in `read'
        from (irb):2
        from /home/sagar/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
1.9.3p0 :003 >

但是当我这样做时

1.9.3p0 :003 > master = CSV.open("./public/jobs/in/Appexchange_Applications_Companies_487.csv","r")
 => <#CSV io_type:File io_path:"./public/jobs/in/Appexchange_Applications_Companies_487.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\n" quote_char:"\"">
1.9.3p0 :004 >

我只想知道为什么会发生这种情况以及解决方案是什么。我想读取 csv,因为它返回该 csv 的数组。因此,如果我以第一种方式读取文件,例如

master = CSV.read("./public/jobs/in/Appexchange_Applications_Companies_487.csv")

它返回给我一个数组

1.9.3p0 :008 > master.class
 => Array

但在第二种情况下,类是 CSV。以第一种方式读取 csv 的解决方案是什么。

4

1 回答 1

0

关于错误:首先,确保您使用正确的字符编码。如果你这样做了,那么你的 csv 文件中可能有无效的数据。您可能可以使用 iconv 修复它(请参阅 Chetan Muneshwar 发布的链接)。

关于你问题的第二部分:CSV.open只是打开文件进行阅读,但还没有阅读。CSV.read将打开文件,读取其内容,然后再次关闭它。因此,只需从文件中获取数据,请使用CSV.read.

于 2012-08-14T15:23:10.177 回答