这就是我正在做的:
csv = CSV.open(file_name, "r")
我用它来测试:
line = csv.shift
while not line.nil?
puts line
line = csv.shift
end
我遇到了这个:
ArgumentError: invalid byte sequence in UTF-8
我读了在这里回答 https://stackoverflow.com/a/4697471/724516这就是我尝试过的
csv = CSV.open(file_name, "r", encoding: "windows-1251:utf-8")
我遇到了以下错误:
Encoding::UndefinedConversionError: "\x98" to UTF-8 in conversion from Windows-1251 to UTF-8
然后我遇到了一个 Ruby 宝石 - charlock_holmes。我想我会尝试使用它来查找源编码。
CharlockHolmes::EncodingDetector.detect(File.read(file_name))
=> {:type=>:text, :encoding=>"windows-1252", :confidence=>37, :language=>"fr"}
所以我这样做了:
csv = CSV.open(file_name, "r", encoding: "windows-1252:utf-8")
仍然得到这个:
Encoding::UndefinedConversionError: "\x8F" to UTF-8 in conversion from Windows-1252 to UTF-8