相关疑难解决方法(0)

如何避免在读取文件时跳过UTF-8 BOM

我正在使用最近添加了Unicode BOM头(U + FEFF)的数据源,而我的rake任务现在被它搞砸了.

我可以跳过前3个字节,file.gets[3..-1]但有没有更优雅的方式来读取Ruby中的文件,无论BOM是否存在,都能正确处理?

ruby unicode byte-order-mark file

40
推荐指数
2
解决办法
2万
查看次数

Ruby无法解析CSV文件:CSV :: MalformedCSVError(第1行中的非法引用)

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)

ruby csv malformed

22
推荐指数
6
解决办法
4万
查看次数

标签 统计

ruby ×2

byte-order-mark ×1

csv ×1

file ×1

malformed ×1

unicode ×1