FileHelpers nested quotes and commas - parsing error

时间秒杀一切 提交于 2019-12-31 04:34:26

问题


I'm trying to parse a CSV file from hell, using the fantastic FileHelpers library.

It's failing to handle a row of the form:

"TOYS R"" US"," INC.""",fld2,fld3,"<numberThousands>","<numberThousands>","<numberThousands>",fld7,

FileHelper is very good at handling number fields in 'thousands' format (using a custom formatter), even when wrapped in quotes, trailing commas etc, however it's causing issues with the first field.

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

This field includes both nested quotes and nested commas. FileHelper doesn't know how to handle this and is splitting it into two separate fields, which subsequently causes an exception to be thrown.

Are there any recommended ways to handle this?


回答1:


First, you need to make all of your fields optionally quoted.

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

Then you need replace the escaped delimiters with something else (e.g., a single quote) in a BeforeReadRecord event.

var engine = new FileHelperEngine<MyFileHelpersSpec>();

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


来源:https://stackoverflow.com/questions/21134981/filehelpers-nested-quotes-and-commas-parsing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!