FileHelpers嵌套引号和逗号 – 解析错误

我正在尝试使用奇妙的FileHelpers库从地狱解析CSV文件。

它无法处理表格的一行:

"TOYS R"" US"," INC.""",fld2,fld3,"","","",fld7, 

FileHelper非常擅长处理“千种”格式的数字字段(使用自定义格式化程序),即使用引号,尾随逗号等包装,但它会导致第一个字段出现问题。

 "TOYS R"" US"," INC.""",fld2,... 

该字段包括嵌套引号和嵌套逗号。 FileHelper不知道如何处理它并将其拆分为两个单独的字段,这随后会导致抛出exception。

有没有推荐的方法来处理这个?

首先,您需要选择引用所有字段。

 [DelimitedRecord(",")] public class contactTemplate { [FieldQuoted('"', QuoteMode.OptionalForBoth)] public string CompanyName; [FieldQuoted('"', QuoteMode.OptionalForBoth)] public string fld2; // etc... } 

然后,您需要在BeforeReadRecord事件中使用其他内容(例如,单引号)替换转义分隔符。

 var engine = new FileHelperEngine(); engine.BeforeReadRecord += (sender, args) => args.RecordLine = args.RecordLine.Replace(@"""", "'");