由于红宝石1.9,CSV使用可以进行编码,如果你使用的方法,如解析器:
::foreach, ::open, ::read, and ::readlines.
例如:CSV.read('path/to/file', encoding: "windows-1252:UTF-8")尝试在windows-1252中读取文件并返回一个包含utf-8编码字符串的数组.
如果字符集之间的编码转换有未定义的字符,则给出一个Encoding::UndefinedConversionError.
String.encode方法有一些很好的args来处理这个未定义的字符:
str = str.encode('UTF-8', invalid: :replace, undef: :replace, replace: "" )
有没有办法在使用CSV解析器的字符集之间使用这种替换规则进行未定义的转换?
谢谢.