csv icon indicating copy to clipboard operation
csv copied to clipboard

Parse csv content is not correct

Open quangson91 opened this issue 1 year ago • 2 comments

Hi, Thank you for your hardword to make a great lib.

Currently, I found an issue with following csv content:

"A B", "C, D"

I think the result should be two fields: A B & C, D. But actually, the lib parse result as three fields: A B, C and D;


Here is my sample code:

import 'package:csv/csv.dart';

void main() {
  final rows = const CsvToListConverter().convert('"A B", "C, D"\r\n');
  for (final row in rows) {
    print(row.join('_ '));
  }
}


Output

A B_  C_  D

quangson91 avatar Feb 18 '24 03:02 quangson91

Because of the space after the first , the library does not recognize " as a quote character.

This however also raises the question if we really want to simply ignore quote characters, if they appear inside unquoted strings.

We definitely shouldn't ignore whitespace. (and papa parse doesn't either: https://www.papaparse.com/demo ) I do intend to add the possibility to pre- and post-process data, which would take care of this.

Ignoring the quote characters is IMO a bug. Papa parse also doesn't ignore the quote character.

close2 avatar Feb 19 '24 08:02 close2

@close2 Thank for your fast reponse.

I also think it should consider a bug. After read the whole code, I think we can simply fix by change: https://github.com/close2/csv/blob/master/lib/src/csv_parser.dart#L391

From

    // If we are not yet inside a string, we are now
    if (!_insideString) {
      _insideString = true;
      _insideQuotedString = true;
    }

To

    // We are now inside quote string.
    _insideString = true;
    _insideQuotedString = true;

How do you think?

quangson91 avatar Feb 19 '24 08:02 quangson91