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

Escape comma inside a column

Open jkevinturado opened this issue 3 years ago • 14 comments

Hi,

My raw data got a comma from a certain field, While I'm parsing the data it cause me to have a new column instead. How do you escape a comma inside a column to prevent this?

Here is a example of raw csv data

"[email protected], [email protected]",email.org,none,bad now the data returned from me is

[[email protected],[email protected],email.org,none,bad]

What I'm expecting is

["[email protected],[email protected]",email.org,none,bad]

I tried to escape the comma by escape: '\\,' but it doesn't help

jkevinturado avatar Jun 17 '22 10:06 jkevinturado

My understanding is: "[email protected], [email protected]",email.org,none,bad is the source and you expect ["[email protected],[email protected]",email.org,none,bad], right ?

This seems like the default behavior to me. I checked and can confirm with our online tool

image image

wdavidw avatar Jun 17 '22 13:06 wdavidw

Yes that is correct, is this a default config?

jkevinturado avatar Jun 18 '22 02:06 jkevinturado

Hi,

I am escaping the double quotes and I need to escape that because some of the fields do have double quotes inside the field for example:

image

So this is my config btw:

image

jkevinturado avatar Jun 20 '22 03:06 jkevinturado

Hi, I'm using the default config.

Now the error is: Invalid Closing Quote: got "d" at line 1 instead of delimiter, record delimiter, trimable character (if activated) or comment

Because I have this double quote inside a field: image

jkevinturado avatar Jun 20 '22 04:06 jkevinturado

The screenshot I shared use the default config.

wdavidw avatar Jun 20 '22 06:06 wdavidw

Please share a minimalist and complete example for additionnal help.

wdavidw avatar Jun 20 '22 07:06 wdavidw

Hi,

I was using the default config, I'm okay with the first issue which has the comma inside, now I have double quotes inside the field and it says image

Here is the input please focus on the 2nd line which has the double quotes inside the field

image

jkevinturado avatar Jun 20 '22 08:06 jkevinturado

Honestly, I don't think there is much we can do to handle such a scenario. The first quote place the parser in quoting mode and second quote is not escape which makes the field invalid

wdavidw avatar Jun 20 '22 08:06 wdavidw

I can actually escape the comma, using the option escape. Can I escape multiple characters?

I've tried escape: ',"' but I didn't work

jkevinturado avatar Jun 20 '22 09:06 jkevinturado

Yes, escape may be composed of multiple characters: https://github.com/adaltas/node-csv/blob/master/packages/csv-parse/test/option.escape.coffee#L69-L79

wdavidw avatar Jun 20 '22 10:06 wdavidw

how?

jkevinturado avatar Jun 20 '22 10:06 jkevinturado

Look at the tests with the link above, I based my answer on them.

wdavidw avatar Jun 20 '22 11:06 wdavidw

it didn't work, I think the link above you've provided will work if the character is contiguous.

Mine have to escape comma and double quotes just like this escape: ',"' unfortunately this doesn't work also as well

jkevinturado avatar Jun 20 '22 13:06 jkevinturado