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的解決方案。
UTF-8中的csv文件?如果不是,您可以通過將其作爲第二個參數傳遞給CSV.read來指定編碼。 – jlundqvist 2012-08-14 12:53:37
ruby iconv可以處理編碼問題。 請看看http://stackoverflow.com/questions/1793284/uploaded-file-char-set-conversion-with-ruby – 2012-08-14 13:56:33