danielgindi/node-csv-reader

Error in the parser. Issue handling "value \" with quote"

Opened this issue · 1 comments

file.csv

test,ok
123,"T-250 148\" Med Rf 9000 GVWR Sliding RH Dr"

If you read the file

const Fs = require('fs');
const CsvReadableStream = require('csv-reader');

let inputStream = Fs.createReadStream('./file.csv', 'utf8');

inputStream
  .pipe(new CsvReadableStream({ parseNumbers: true, parseBooleans: true, asObject: true }))
  .on('data', function (row) {
    console.log('A row arrived: ', row);
  })
  .on('end', function () {
    console.log('No more rows!');
  });
{ test: 123, ok: 'T-250 148\\ Med Rf 9000 GVWR Sliding RH Dr"' }

The current returned value of field ok is (Double back slashe and quote at the end):

T-250 148\\ Med Rf 9000 GVWR Sliding RH Dr"

But it should be more like that :

T-250 148\" Med Rf 9000 GVWR Sliding RH Dr

Using PHP I tried to write and read that specific value using internal csv functions:

$file = fopen(__DIR__ . '/file.csv', 'w');

$row = array('test' => '123', 'ok' => 'T-250 148\" Med Rf 9000 GVWR Sliding RH Dr');

fputcsv($file, array_keys($row));
fputcsv($file, $row);

fclose($file);

$file = fopen(__DIR__ . '/file.csv', 'r');
print_r(fgetcsv($file));
print_r(fgetcsv($file));
fclose($file);

The output file is
file.csv

test,ok
123,"T-250 148\" Med Rf 9000 GVWR Sliding RH Dr"

And the value I received retrieve is:

T-250 148\" Med Rf 9000 GVWR Sliding RH Dr

Possible solution

As I know different way to encode quotes in csv value.
One is using a escape string \

Then encoding

T-250 148\" Med Rf 9000 GVWR Sliding RH Dr

Then resulting csv value look like that

"T-250 148\" Med Rf 9000 GVWR Sliding RH Dr"

If we don't use escape by default quote are doubled.

"T-250 148\"" Med Rf 9000 GVWR Sliding RH Dr"

Maybe you can add an option escape: '\'

  .pipe(new CsvReadableStream({ parseNumbers: true, parseBooleans: true, escape: '\\', asObject: true }))

Do you want to open a PR for that?
Also I'm pretty sure that programs like Excel will not work this way. Some scripting languages may "support" backslash escaping in CSV due to it being implemented in the core of string processing, but that's not the expected behavior when working with spreadsheet software.