danielgindi/node-csv-reader

Parsing bug

philip-nicholls opened this issue · 2 comments

test.csv

"A", "1"
"2", "B"
"C", "D"

parse.js

let inputStream = fs.createReadStream(__dirname + '/test.csv', 'utf8');
inputStream
    .pipe(new CsvReadableStream())
    .on('data', function (row) {
        console.log("DATA", row);
    })
    .on('end', function () {
        console.log("END");
    })
    .on('error', function (e) {
        reject(e);
    });

Output

DATA [ 'A', ' "1"' ]
DATA [ '2', ' "B"' ]
DATA [ 'C', ' "D"' ]
END

Your parser doesn't like whitespace between the columns.

yeya commented

I guess Microsoft Excel "doesn't like" it too.
It reads your test.csv just the same...
image

What do you think should be the result, and why?

This parser behaves like Excel, right, but it is only because this is the correct behavior. Other behaviors would create unpredictable results.
This is a comma separated file, not "comma and space separated".
Now when there are special characters, we need to put the value in quotes.
And quotes could appear inside a value.
Imagine a value that begins with a space and a quote... That's just a standard value, unwrapped.
Btw, I have never seen any piece of software create csvs with spaces after the commas.