2012-08-14 57 views
2
[[email protected] 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 > 

無效字節序列但是當我做紅寶石CSV錯誤:UTF-8

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的解決方案。

+0

UTF-8中的csv文件?如果不是,您可以通過將其作爲第二個參數傳遞給CSV.read來指定編碼。 – jlundqvist 2012-08-14 12:53:37

+0

ruby​​ iconv可以處理編碼問題。 請看看http://stackoverflow.com/questions/1793284/uploaded-file-char-set-conversion-with-ruby – 2012-08-14 13:56:33

回答

0

關於錯誤:首先,確保您使用的是正確的字符編碼。如果你這樣做,那麼你的csv文件中可能有無效的數據。您可以使用iconv修復它(請參閱由Chetan Muneshwar發佈的鏈接)。

關於你的問題的第二部分:CSV.open只是打開文件閱讀,但沒有閱讀。 CSV.read將打開文件,讀取其內容並再次關閉。因此,只需從文件中使用CSV.read即可獲取數據。