michalmonday / CSV-Parser-for-Arduino

It turns CSV string into an associative array (like dict in python)
MIT License
58 stars 12 forks source link

Quotes handling is broken #17

Closed KingBoomie closed 1 year ago

KingBoomie commented 1 year ago

For example, when parsing csv

"Ajatempel (UTC)";"Kuup�ev (Eesti aeg)";"NPS Eesti"
"1590958800";"01.06.2020 00:00";"3.88"
"1590962400";"01.06.2020 01:00";"4.54"
"1590966000";"01.06.2020 02:00";"4.16"
"1590969600";"01.06.2020 03:00";"4.54"
"1590973200";"01.06.2020 04:00";"4.56"
"1590976800";"01.06.2020 05:00";"3.97"
"1590980400";"01.06.2020 06:00";"5.63"
"1590984000";"01.06.2020 07:00";"29.49"
"1590987600";"01.06.2020 08:00";"40.08"
"1590991200";"01.06.2020 09:00";"43.75"
"1590994800";"01.06.2020 10:00";"45.4"
"1590998400";"01.06.2020 11:00";"44.96"
"1591002000";"01.06.2020 12:00";"45.08"
"1591005600";"01.06.2020 13:00";"44.89"
"1591009200";"01.06.2020 14:00";"44.46"

with


static char buff[] = "\"Ajatempel (UTC)\";\"Kuup�ev (Eesti aeg)\";\"NPS Eesti\"\n"
                         "\"1590883200\";\"31.05.2020 03:00\";\"1.96\"\n"
                         "\"1590886800\";\"31.05.2020 04:00\";\"1.91\"\n"
                         "\"1590890400\";\"31.05.2020 05:00\";\"1.76\"\n"
                         "\"1590894000\";\"31.05.2020 06:00\";\"1.91\"\n"
                         "\"1590897600\";\"31.05.2020 07:00\";\"1.94\"\n"
                         "\"1590901200\";\"31.05.2020 08:00\";\"2.7\"\n"
                         "\"1590904800\";\"31.05.2020 09:00\";\"2.78\"\n"
                         "\"1590908400\";\"31.05.2020 10:00\";\"4.07\"\n"
                         "\"1590912000\";\"31.05.2020 11:00\";\"4.09\"\n"
                         "\"1590915600\";\"31.05.2020 12:00\";\"4.09\"\n"
                         "\"1590919200\";\"31.05.2020 13:00\";\"4.05\"\n"
                         "\"1590922800\";\"31.05.2020 14:00\";\"4.01\"\n"
                         "\"1590926400\";\"31.05.2020 15:00\";\"2.93\"\n"
                         "\"1590930000\";\"31.05.2020 16:00\";\"1.67\"\n"
                         "\"1590933600\";\"31.05.2020 17:00\";\"3.28\"\n"
                         "\"1590937200\";\"31.05.2020 18:00\";\"4.07\"\n"
                         "\"1590940800\";\"31.05.2020 19:00\";\"4.02\"\n"
                         "\"1590944400\";\"31.05.2020 20:00\";\"4.5\"\n"
                         "\"1590948000\";\"31.05.2020 21:00\";\"16.01\"\n"
                         "\"1590951600\";\"31.05.2020 22:00\";\"22.71\"\n"
                         "\"1590955200\";\"31.05.2020 23:00\";\"20.98\"\n"
                         "\"1590958800\";\"01.06.2020 00:00\";\"3.88\"\n"
                         "\"1590962400\";\"01.06.2020 01:00\";\"4.54\"\n"
                         "\"1590966000\";\"01.06.2020 02:00\";\"0.16\"";
CSV_Parser cp(buff, "Lsf", true, ';');
cp.parseLeftover();

long *timestamps = (long *)cp[0];
char ** dates = (char**)cp[1];
float * prices = (float*)cp[2];

for (int i = 0; i < cp.getRowsCount(); i++) {
      Serial.print(timestamps[i]);          Serial.print(" - ");
      Serial.print(dates[i]);             Serial.print(" - ");
      Serial.print(prices[i]);           Serial.print(" - ");
      Serial.println("");
  }

output looks something like

1590883200 -  - 31.05 -
0 - 1.96 - 1590886784.00 -
0 - 31.05.2020T04:00 - 0.00 -
1 - 1590890400 - 0.00 -
31 -  - 1.76 -
1590894000 -  - 31.05 -
0 - 1.91 - 1590897664.00 -
0 - 31.05.2020T07:00 - 0.00 -
1 - 1590901200 - 0.00 -
31 -  - 2.70 -

expected output

1590883200 -  31.05.2020 03:00  - 1.96 -
1590886800 -  31.05.2020 04:00  - 1.91 -
1590890400 -  31.05.2020 05:00  - 1.76 -
1590894000 -  31.05.2020 06:00  - 1.91 -
1590897600 -  31.05.2020 07:00  - 1.94 -
1590901200 -  31.05.2020 08:00  - 2.70 -
1590904800 -  31.05.2020 09:00  - 2.78 -

I traced the problem down to quotes, because in my case they aren't actually necessary, and removing them made the library function.

michalmonday commented 1 year ago

Thank you very much, 1.0.1 version should have it fixed.

It was a silly mistake (in one condition ',' was hardcoded instead of using delimiter variable. Luckily I found another bug too (related to including multiple quote chars next to each other within the parsed string itself). Both fixed in this commit: https://github.com/michalmonday/CSV-Parser-for-Arduino/commit/8f6024c45920f54821a79e383cef6e87db255e59

KingBoomie commented 1 year ago

Thanks for the quick fix! Confirmed fixed.