本文介绍了如何在异常后继续处理Ruby中的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to process some very large tab-separated files. The process is:

  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

但是,当我执行这个,我得到以下错误:

However, when I execute this I get the following error:

Unable to process the file: /Users/XXXXX_2013-06-12.tsv because Illegal quoting in line 153.

这些文件太大,我不能进去并修复错误行。我想让进程继续循环和处理文件,即使有错误行。

The files are too big for me to go in and fix the error rows. I would like the process to continue the loop and process the file even if there are error rows.

有任何建议吗?

谢谢。

推荐答案

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

甚至可以在循环之前用logger

you can even log it with logger

进行记录:

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

使用

rescue error_log.info(row.to_s)


$ b 这里)

..或者只是抢救完整的文件解析过程

..or just rescue full file parsing procedure

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

这篇关于如何在异常后继续处理Ruby中的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:30