Skip to content

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

@ylupien

Description

@ylupien

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 }))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions