FileHelpers nested quotes and comma - parse error

I am trying to parse a CSV file from hell using the fantastic FileHelpers library.

Unable to parse form line:

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

      

FileHelper is very good at handling thousands of numeric fields (using custom formatting), even if they are enclosed in quotes, commas, etc., however it causes problems with the first field.

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

      

This field contains both nested quotes and nested commas. FileHelper doesn't know how to handle this and splits it into two separate fields, which subsequently throws an exception.

Are there any recommended ways to do this?

+1


source to share


1 answer


First, you need to list all of your fields that can be specified.

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

      



Then you need to replace the escaped separators with something else (like a single quote) in the event BeforeReadRecord

.

var engine = new FileHelperEngine<MyFileHelpersSpec>();

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

      

+5


source







All Articles