如何进一步处理导致Ruby FasterCSV库引发MalformedCSVError的数据行? [英] How can I further process the line of data that causes the Ruby FasterCSV library to throw a MalformedCSVError?

查看:77
本文介绍了如何进一步处理导致Ruby FasterCSV库引发MalformedCSVError的数据行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传入的数据文件包含格式错误的CSV数据(例如,未转义的引号)以及(有效)CSV数据(例如,包含新行的字段).如果检测到CSV格式错误,我想对该数据使用其他例程.

The incoming data file(s) contain malformed CSV data such as non-escaped quotes, as well as (valid) CSV data such as fields containing new lines. If a CSV format error is detected I would like to use an alternative routine on that data.

带有以下示例代码(为简化起见,已缩写)

With the following sample code (abbreviated for simplicity)

FasterCSV.open( file ){|csv|
  row = true
  while row
    begin
      row = csv.shift
      break unless row
      # Do things with the good rows here...

    rescue FasterCSV::MalformedCSVError => e
      # Do things with the bad rows here...
      next
    end
  end
}

csv.shift方法中引起了MalformedCSVError.如何从救援子句中访问导致错误的数据?

The MalformedCSVError is caused in the csv.shift method. How can I access the data that caused the error from the rescue clause?

推荐答案

require 'csv' #CSV in ruby 1.9.2 is identical to FasterCSV

# File.open('test.txt','r').each do |line|
DATA.each do |line|
  begin
    CSV.parse(line) do |row|
      p row #handle row
    end
  rescue  CSV::MalformedCSVError => er
    puts er.message
    puts "This one: #{line}"
    # and continue
  end
end

# Output:

# Unclosed quoted field on line 1.
# This one: 1,"aaa
# Illegal quoting on line 1.
# This one: aaa",valid
# Unclosed quoted field on line 1.
# This one: 2,"bbb
# ["bbb", "invalid"]
# ["3", "ccc", "valid"]   

__END__
1,"aaa
aaa",valid
2,"bbb
bbb,invalid
3,ccc,valid

只需将文件逐行送入FasterCSV并挽救错误.

Just feed the file line by line to FasterCSV and rescue the error.

这篇关于如何进一步处理导致Ruby FasterCSV库引发MalformedCSVError的数据行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆