Home > Blockchain >  CSV quoted values with line break inside data
CSV quoted values with line break inside data

Time:12-19

This question is specific to ChoETL CSV reader

Take this example

"Header1","Header2","Header3"
"Value1","Val
ue2","Value3"

(Notepad screenshot)
enter image description here

  • All headers and values are quoted
  • There's a line break in "Value2"

I've been playing with ChoETL options, but I can't get it to work:

   foreach (dynamic e in new
                ChoCSVReader(@"test.csv")
                .WithFirstLineHeader()
                .MayContainEOLInData(true)
                .MayHaveQuotedFields()

                //been playing with these too
                //.QuoteAllFields()
                // .ConfigureHeader(c => c.IgnoreColumnsWithEmptyHeader = true)
                //.AutoIncrementDuplicateColumnNames()
                //.ConfigureHeader(c => c.QuoteAllHeaders = true)
                //.IgnoreEmptyLine()

                )
            {
                System.Console.WriteLine(e["Header1"]);
            }

This fails with:

Missing 'Header2' field value in CSV file

The error varies depending on the reader configuration

What is the correct configuration to read this text?

CodePudding user response:

It is bug in handling one of the cases (ie. header having quotes - csv2 text). Applied fix. Take the ChoETL.NETStandard.1.2.1.35-beta1 package and give it a try.

string csv1 = @"Header1,Header2,Header3
""Value1"",""Val
ue2"",""Value3""";

string csv2 = @"""Header1"",""Header2"",""Header3""
""Value1"",""Val
ue2"",""Value3""";

string csv3 = @"Header1,Header2,Header3
Value1,""Value2"",Value3";

using (var r = ChoCSVReader.LoadText(csv1)
    .WithFirstLineHeader()
    .MayContainEOLInData(true)
    .QuoteAllFields())
    r.Print();

using (var r = ChoCSVReader.LoadText(csv2)
    .WithFirstLineHeader()
    .MayContainEOLInData(true)
    .QuoteAllFields())
    r.Print();

using (var r = ChoCSVReader.LoadText(csv3)
    .WithFirstLineHeader()
    .MayContainEOLInData(true)
    .QuoteAllFields())
    r.Print();

Sample fiddle: https://dotnetfiddle.net/VubCDR

  • Related