Handling quotes with FileHelpers

7.5k Views Asked by At

I'm using the excellent FileHelpers library to import many csv files, but run into a problem. I have a csv file with these three example lines

id,text,number
120,"good line this one",789
121,""not good" line", 4456
122,,5446

and this (example) class

  [IgnoreFirst(1)]
  [IgnoreEmptyLines()]
  [DelimitedRecord(",")]
  public sealed class JOURNAL
  {
    public Int32 ID;

    [FieldQuoted('"', QuoteMode.AlwaysQuoted, MultilineMode.NotAllow)]
    public string TEXT;

    public Int32? NUMBER;
  }

The problem with QuoteMode.AlwaysQuoted is that ID 122 will fail with error:

The field 'TEXT' not begin with the QuotedChar at line 3. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field

Switching to QuoteMode.OptionalForRead will fail with an error for id 121:

The field TEXT is quoted but the quoted char: " not is just before the separator (You can use [FieldTrim] to avoid this error)

So how can I handle a csv that has empty fields with no quotes AND quoted text fields with extra quotes in the text?

1

There are 1 best solutions below

5
On

That looks like a case that we don't support, let me add a test case and make it work in both modes, for the first one we need to validate if semantic is correct, ie. if QuoteMode.AlwaysQuoted can allow ,, or must be ,"", but the second option must work :) Cheers