传入的数据文件包含格式错误的 CSV 数据,例如非转义引号,以及(有效)CSV 数据,例如包含新行的字段。如果检测到 CSV 格式错误,我想对该数据使用替代例程。
使用以下示例代码(为简单起见缩写)
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
}
MalformedCSVError 是在 csv.shift 方法中引起的。如何从救援子句访问导致错误的数据?