Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling quotes with FileHelpers

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?

like image 911
edosoft Avatar asked Feb 22 '11 10:02

edosoft


1 Answers

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

like image 54
Marcos Meli Avatar answered Sep 22 '22 01:09

Marcos Meli