Ruby:一个编码数据到另一个带有多行的编码csv?

时间:2013-01-13 16:29:39

标签: ruby csv encoding

如何输出和转换编码?

输入:UTF-8上的数据对象

输出:CP932上的CSV(不是特定的cp932,点是数据包括'to encoding'中的未定义字符串)

# coding: utf-8
# input values with utf-8
# output csv with cp932
require 'csv'
headers = %w|sa sa ta tsu|
bodys = []
bodys << ["ka", "ra", "揚げ", "\u00A0"] # \u00a0 is valid in UTF-8, is undef in cp932
bodys << [1000, "1", nil, "\u00A0"]

path_to_file = './file.csv'
CSV.open(path_to_file, 'w:cp932') do |csv|
  csv << headers
  bodys.each do |values|
    csv << values
  end
end

临时解决方案

CSV.open(path_to_file, 'w:cp932') do |csv|
  csv << headers
  bodys.each do |values|
    csv << values.map {|v| v.instance_of?(String) ?
                       v.encode('cp932', 'utf-8', invalid: :replace, undef: :replace, replace: ' ') :
                       v }
  end
end

END

$ ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2]
Encoding::UndefinedConversionError: U+00A0 from UTF-8 to Windows-31J
from /Users/sane/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/csv.rb:1736:in `write'
$ ruby -v
ruby 2.0.0dev (2013-01-07 trunk 38733) [x86_64-darwin11.4.2]
Encoding::UndefinedConversionError: U+00A0 from UTF-8 to Windows-31J
from /Users/sane/.rbenv/versions/2.0.0-rc1/lib/ruby/2.0.0/csv.rb:1788:in `write'

0 个答案:

没有答案