在尝试解析数组,AR模型导入等时,似乎有一个常见问题是有错误的CSV文件。我还没有找到除MS Excel和save as
之外的其他工作解决方案一天(不够好!)。
在60,000行外部提供的每日更新的csv文件中,出现错误:CSV::MalformedCSVError: Illegal quoting in line 95.
(作为示例)。我很高兴跳过/忘记格式错误的行(即它只有1/60000的重要性)。
首次尝试使用CSV.foreach
或类似内容,只需使用begin
rescue
next
end
即可跳过此错误。没有骰子。我希望这个接受的答案更加冗长:CSV.read Illegal quoting in line x(即"只是自己阅读文件" - 我想我在下面尝试过)。
这个SO Q& A(How can I further process the line of data that causes the Ruby FasterCSV library to throw a MalformedCSVError?)似乎有希望,但是接受的答案并不......完全......在我看似相似的情况下工作(例如清晰度修改),通过rake执行任务:
file_path = "filename.csv"
my_array = []
File.open(file_path).each do |line| # `foreach` instead of `open..each` does the same
begin
CSV.parse(line) do |row|
my_array << row
end
rescue CSV::MalformedCSVError => er
puts er.message
counter += 1
next
end
counter += 1
puts "#{counter} read success"
end
输出=&gt;
1 read success
2 read success
...
94 read success
Illegal quoting in line 1 # strange that it says `line 1` vs `95`, which may be the crux of what I do not understand here (e.g. some kind of look ahead error)
96 read success
...
60000 read success
# I checked using `line.inspect`, and the 60000th row is indeed being read/inspected
rake aborted!
CSV:MalformedCSVError: Illegal quoting in line 95
答案 0 :(得分:1)
您的解决方案有效。预期结果驻留在变量my_array中。