阅读时Ruby CSV UTF8编码错误

这就是我在做的事情:

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 

我在这里读到答案 ,这就是我的尝试

 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 gem – 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 

看起来您在检测文件的有效编码时遇到问题。 CharlockHolmes为您提供了有用的提示:confidence=>37 ,这意味着检测到的编码可能不正确。

基于错误消息和来自https://github.com/MacRuby/MacRuby/blob/master/test-mri/test/ruby/test_transcode.rb的 test_transcode.rb我发现了通过这两个错误消息的编码。 在String#encode帮助下,它很容易测试:

 "\x8F\x98".encode("UTF-8","cp1256") # => "ڈک" 

您的问题看起来与文件严格相关,而不是ruby。

如果我们不确定使用哪种编码并且可以同意松散某些字符,我们可以使用:invalid:undef params for String#encode :undef String#encode ,在这种情况下:

 "\x8F\x98".encode("UTF-8", "CP1250",:invalid => :replace, :undef => :replace, :replace => "?") # => "Ź?" 

另一种方法是使用Iconv *//IGNORE选项进行目标编码:

 Iconv.iconv("UTF-8//IGNORE","CP1250", "\x8F\x98") 

作为CharlockHolmes的源编码建议应该是相当不错的。

PS。 String.encode是在ruby 1.9中引入的。 使用ruby 1.8,您可以使用Iconv