如何在exception后继续在Ruby中处理块?

我正在尝试处理一些非常大的制表符分隔文件。 过程是:

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. 

这些文件太大了,我无法进入并修复错误行。 我希望这个过程继续循环并处理文件,即使有错误行。

有什么建议?

谢谢。

只是... rescue nil导致错误的行

你甚至可以用记录器记录它

在循环之前:

 error_log ||= Logger.new("#{Rails.root}/log/my.log") 

在循环内而不是仅仅rescue nil使用

 rescue error_log.info(row.to_s) 

如果您在文件开始解析之前得到错误(在.foreach过程之前),您可以将其作为原始文件打开并稍后将其读取为CSV – 在循环内(如此处所述)

..或只是救援完整的文件解析程序

  CSV.foreach(file, :col_sep => "\t") do |row| ... end rescue error_log.info(row.to_s)