我正在尝试处理一些非常大的制表符分隔文件。这个过程是:
begin
Dir["#{@data_path}*.tsv"].each do |file|
begin
CSV.foreach(file, :col_sep => "\t") do |row|
# assign columns to model and save
end
@log.info("Loaded all files into MySQL database illu.datafeeds")
rescue Exception => e
@log.warn("Unable to process the data feed: #{file} because #{e.message}")
next
end
end
然而,当我执行此操作时,我收到以下错误:
Unable to process the file: /Users/XXXXX_2013-06-12.tsv because Illegal quoting in line 153.
文件太大,我无法进入并修复错误行。我希望该过程继续循环并处理文件,即使存在错误行。
有什么建议吗?
感谢。
答案 0 :(得分:4)
只有... rescue nil
导致错误的行
您甚至可以使用记录器
进行记录 循环之前:
error_log ||= Logger.new("#{Rails.root}/log/my.log")
在循环内部而不仅仅是rescue nil
使用
rescue error_log.info(row.to_s)
如果您在文件开始解析之前收到错误(在.foreach过程之前),您可以将其作为原始文件打开并稍后将其读取为CSV - 在循环内部(如提到的here)
..或者只是救出完整的文件解析程序
CSV.foreach(file, :col_sep => "\t") do |row|
...
end rescue error_log.info(row.to_s)