我尝试了各种迭代:编码和选项但没有成功。该文件是使用python csv模块创建的,并且启用了quote_all选项。它使用默认的quote_char ='“'和encoding ='ascii';我已经在ruby脚本中匹配了这些设置,但每当我尝试读取此文件时,我都会收到以下错误:
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1894:in `block (2 levels) in shift': Missing or stray quote in line 1 (CSV::MalformedCSVError)
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1887:in `each'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1887:in `block in shift'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1849:in `loop'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1849:in `shift'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1791:in `each'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1805:in `to_a'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1805:in `read'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1411:in `block in read'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1354:in `open'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/csv.rb:1411:in `read'
这是csv文件的第1行:
"1","
因为第2行以换行符开头
看起来这可能与源文件有关;切换一些选项可以让我了解问题所在。
答案 0 :(得分:0)
我想提供适合我的答案:我通过删除标题在不知不觉中省略了一些关键的格式化指令。添加它们允许文件正常解析,但我不希望解析器处理标题,所以我必须找到一个选项来做到这一点,而不会破坏csv gem,我发现在这里:{{3} }。