我正在使用最近添加了Unicode BOM头(U + FEFF)的数据源,而我的rake任务现在被它搞砸了.
我可以跳过前3个字节,file.gets[3..-1]但有没有更优雅的方式来读取Ruby中的文件,无论BOM是否存在,都能正确处理?
Ubuntu 12.04 LTS
Ruby ruby 1.9.3dev(2011-09-23修订版33323)[i686-linux]
Rails 3.2.9
以下是我收到的CSV文件的内容:
"date/time","settlement id","type","order id","sku","description","quantity","marketplace","fulfillment","order city","order state","order postal","product sales","shipping credits","gift wrap credits","promotional rebates","sales tax collected","selling fees","fba fees","other transaction fees","other","total"
"Mar 1, 2013 12:03:54 AM PST","5481545091","Order","108-0938567-7009852","ALS2GL36LED","Solar Two Directional 36 Bright White LED Security Flood Light with Motion Activated Sensor","1","amazon.com","Amazon","Pasadena","CA","91104-1056","43.00","3.25","0","-3.25","0","-6.45","-3.75","0","0","32.80"
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试解析CSV文件时,我收到错误:
1.9.3dev :016 > options = { col_sep: ",", quote_char:'"' }
=> {:col_sep=>",", :quote_char=>"\""}
1.9.3dev :022 > CSV.foreach("/tmp/my_data.csv", options) { |row| puts row }
CSV::MalformedCSVError: Illegal quoting in line 1.
from /home/jigneshgohel/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/1.9.1/csv.rb:1925:in `block (2 levels) …Run Code Online (Sandbox Code Playgroud)