3
我有一些Ruby脚本读取一些CSV文件,处理它们,并写出一组(新)CSV文件。我使用Ruby 1.9.2和新的标准'csv'gem(曾经是FasterCSV)。源CSV文件包含非ascii字符(é等),但它们来自Excel,因此编码没有正确标记。具体来说,当我加载文件到红宝石我得到这个:Ruby 1.9使用Excel CSV编码
require 'csv'
t = CSV.table('file.csv',:converters=>nil)
t.to_s.encoding
# encoding is ASCII-8BIT
即使实际的字符串是UTF-8。我的问题是,我似乎无法得到这个字符串,它被标记为ASCII实际转换为UTF-8。当我尝试这样做时:
require 'csv'
t = CSV.table('file.csv',:converters=>nil)
f = File.new('output.csv','w:utf-8')
f.write(t.to_s.force_encoding('utf-8'))
f.close
输出文件仍被列为以ASCII编码。我需要做什么才能使输出文件以UTF-8编码?
请看这个讨论:http://stackoverflow.com/questions/7047944/ruby-read-csv-file-as-utf-8-and-or-convert-ascii-8bit -encoding-to-utf-8 希望这对你有所帮助。 – WarHog
@WhHog谢谢,这正是我正在寻找的。 –