0

I've a data file(csv) which I need to read in c#, but different libraries

  • CsvHelper
  • LumenWorksCsvReader

are fail to accomplish this.

Ex. with LumenWorksCsvReader

using (CsvReader csv = new CsvReader(new StreamReader(path), true))
        {
            int fieldCount = csv.FieldCount;

            string[] headers = csv.GetFieldHeaders();
            while (csv.ReadNextRecord())
            {
                for (int i = 0; i < fieldCount; i++)
                    Console.Write(string.Format("{0} = {1};",
                                  headers[i], csv[i]));
                Console.WriteLine();
            }
        }

Above code broke at reading the Deivce cell in 2nd row. enter image description here

My data is look a like

InvoiceDate,Device
2022/07/16,"i5-10210U 1,6GHz 2x8GB W10P64;14\" 256GB SSD, Einbau"

as you can see the data is perfect, but while reading in c#, everything got failed to accommodate this type of data situation

Manish Jain
  • 217
  • 1
  • 4
  • 16
  • 4
    Show us your attempts; every CSV reader I'm familiar with supports this "quoted fields that contain quotes and commas" out of the box. I'm sure we can easily fix one of your tries, if only we could see them. It will also help get a faster answer because we won't have to write out the whole code for you – Caius Jard Oct 26 '21 at 07:54
  • 1
    ^^ Even a failed attempt can get us started. So, just post your best shot at it. – Fildor Oct 26 '21 at 07:56
  • 1
    Please show us your code and the exact error message. It tends to be rather difficult to debug code which one cannot see! – jason.kaisersmith Oct 26 '21 at 07:56
  • Of course, if you have configured everything correctly and you didn't get a good result then we can reproduce it and maybe make a bug report. I'm sure Josh Close will be very interested to hear CsvHelper not being able to read quoted fields and will jump on fixing it quickly.. – Caius Jard Oct 26 '21 at 08:01
  • Does it work if you replace that string by a simpler one? Like `2022/07/16,"testestest"`? (Just to narrow down causes) – Fildor Oct 26 '21 at 08:15
  • Yes, that works for simple "testtesttest", but my data is bit complicated as you can find in my question – Manish Jain Oct 26 '21 at 08:19
  • I just wanted to rule out that it _isn't_ something with that string per se. Now, what I would do is add portions of that string one by one and try again. Then you should see what part it consideres a "malformation". Once you know what exactly causes the exception, you can start looking for solutions. My bet is on the `\"` ... but that's just a guess. – Fildor Oct 26 '21 at 08:24

1 Answers1

0

As pointed in comments the problem comes from the \" in the device string.

You need to specify the escape parameter in the CsvReader instance like this.

using (CsvReader csv = new CsvReader(new StreamReader(filePath), true, escape: '\\'))
{
   int fieldCount = csv.FieldCount;

   string[] headers = csv.GetFieldHeaders();
   while (csv.ReadNextRecord())
   {
      for (int i = 0; i < fieldCount; i++)
      Console.Write(string.Format("{0} = {1};", headers[i], csv[i]));
      Console.WriteLine();
   }
}
yos
  • 9
  • 3