node-csv icon indicating copy to clipboard operation
node-csv copied to clipboard

Support multiple quote characters when parsing

Open loucadufault opened this issue 1 year ago • 4 comments

Summary

The library should be able to parse CSVs while considering multiple different potential quote character used for each value independently. E.g. allow a CSV mixing both single quote and double quotes.

Motivation

Use cases where CSVs mix values ported over from other formats (e.g. literals in a programming language) which enclose values in single quotes, and manually input. Reconciling the two quote styles would enable parsing the CSV regardless of which (if any) quote character was used.

Alternative

Regex to strip replace single quotes with double quotes via a utility, though this will break quotes in values.

Draft

Let the quote option take in an array of characters, and consider each when parsing the values.

Example unit test:

const records = parse(`
a,"b",'c'
'd',e,"f"
`.trim(), {
  quote: ['"', '\''],
});

assert.deepStrictEqual(
  records, [
    ['a', 'b', 'c'],
    ['d', 'e', 'f']
  ]
);

Additional context

Similar to https://github.com/adaltas/node-csv/issues/400. Perhaps automated detection is a sensible option.

loucadufault avatar May 22 '24 20:05 loucadufault

We do it for some other options, not too hard to implement so no problem to go this direction.

wdavidw avatar May 23 '24 11:05 wdavidw

I can propose a PR, seems the changes would be fairly localized. Lmk if that would be any help

loucadufault avatar May 23 '24 18:05 loucadufault

Agreed, please propose a PR. (not sure what Lmk stands for)

wdavidw avatar May 24 '24 05:05 wdavidw