第1行中的引用非法.(CSV :: MalformedCSVError) [英] Illegal quoting in line 1. (CSV::MalformedCSVError)

查看:47
本文介绍了第1行中的引用非法.(CSV :: MalformedCSVError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试读取使用Selenium WebDriver下载的CSV时,在第1行中出现非法报价.(CSV :: MalformedCSVError).

I'm getting a Illegal quoting in line 1. (CSV::MalformedCSVError) when I try to read a CSV that I download using Selenium WebDriver.

CSV.foreach( "foo.csv" ) do |row|
  # anger :(
end

但是当我复制内容并将其粘贴到新文件中并再次保存时,它就可以正常工作:

but when I copy the contents and paste it into a new file and save it again, it works just fine:

CSV.foreach( "bar.csv" ) do |row|
  # works fine
end

这是有问题的CSV的前5行,以防您...

Here's the first 5 lines of the CSV in question, in case it helps...

"Name","W","L","ERA","GS","G","SV","IP","H","ER","HR","SO","BB","WHIP","K/9","BB/9","FIP","WAR","playerid"
"Craig Kimbrel","5","1","1.79","0","65","35","65.0","42","13","4","95","19","0.95","13.16","2.65","1.84","1.7","6655"
"Aroldis Chapman","2","1","1.93","0","30","27","30.0","18","6","2","47","12","0.99","14.24","3.56","2.22","0.6","10233"
"Greg Holland","5","2","2.39","0","65","34","65.0","47","17","5","83","21","1.05","11.53","2.95","2.48","1.3","7196"
"Kenley Jansen","5","2","2.16","0","65","32","65.0","46","16","6","86","19","1.00","11.97","2.64","2.51","0.9","3096"

我一直无法找到或提出一种方法来正确读取我的原始硒下载的CSV.任何人都遇到这个问题或对我的数据可能有什么问题或如何以编程方式解决此问题有任何想法吗?

I haven't been able to find or come up with a way to get my raw, selenium-downloaded CSV to be read correctly. Anyone run into this or have any ideas on what could be wrong with my data, or how I can fix this programmatically?

谢谢!

推荐答案

您的文件很可能在一开始就具有字节顺序标记 U + FEFF .当您再次复制并粘贴时,您可能会丢失它.

It's very likely that your file has a byte-order mark U+FEFF at the very beginning. You are probably losing it when you copy and paste again.

正确的解决方法是:

CSV.foreach("foo.csv", "r:bom|utf-8") { ... }

这篇关于第1行中的引用非法.(CSV :: MalformedCSVError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆