当我尝试阅读使用Selenium WebDriver下载的CSV时,我获得Illegal quoting in line 1. (CSV::MalformedCSVError)
。
CSV.foreach( "foo.csv" ) do |row|
# anger :(
end
但是当我复制内容并将其粘贴到一个新文件并再次保存时,它可以正常工作:
CSV.foreach( "bar.csv" ) do |row|
# works fine
end
这是有问题的CSV的前5行,如果它有帮助...
"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"
我还没有找到或想出一种方法来正确读取我的原始的,下载selen的CSV。任何人都遇到这个问题,或者对我的数据可能有什么问题有任何想法,或者我如何以编程方式解决这个问题?
谢谢!
答案 0 :(得分:5)
您的文件很可能在一开始就有一个字节顺序标记U+FEFF
。当你再次复制和粘贴时,你可能会丢失它。
正确的解决方案是:
CSV.foreach("foo.csv", "r:bom|utf-8") { ... }