ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

Change the delimiter #6

Closed PPCM closed 6 years ago

PPCM commented 6 years ago

Is it possible to change the delimiter to ; for example ? Excel doesn't parse a CSV with comma as delimiter

ryu1kn commented 6 years ago

Hello @PPCM , what do you mean by excel doesn't parse a CSV?

I created a CSV file (comma separated values) saved with the name test.csv, then opened it with Excel. It all looked fine, as in, different fields appeared in the different columns as expected.

ryu1kn commented 6 years ago

Hi @PPCM , I'm closing this for now as I'm not getting the more information. Feel free to reopen it if you have more things to add :wink:

PPCM commented 6 years ago

Sorry for the long time In my case, Excel 2016 open the file with each line in a simple cell, even if I use the import feature

Sigfried commented 6 years ago

I have the same question: can I use tab delimiters? In my case I'm writing data that is coming from a tsv and has commas and quotes in the data.

ryu1kn commented 6 years ago

Hi @Sigfried . At the moment, no, csv-writer produces only CSV.

Do you actually want to produce TSV, or are you ok with CSV as long as commas/quotes in the data are properly handled? csv-writer should be able to produce a properly formatted CSV file by automatically escaping comma/quote characters (like this).

ryu1kn commented 6 years ago

I didn't know Excel assumes semicolon as a field delimiter in some regions. Given that Excel is widely used software to consumer CSV, I'm going to support this variance even though it's not RFC4180 compliant. Thanks @PPCM & @Sigfried for flagging the issue!

From https://github.com/ryu1kn/csv-writer/pull/8#issuecomment-414106442

Ok, I found that, on Windows, depending on the "Regional and Language Options" settings, Excel assumes different characters for the field delimiter...

Seems we do need to support ; as a delimiter. Now I need to find what is the escaping rule for the field value when ; is used as a separator.

Also from Wikipedia

Adjacent fields must be separated by a single comma. However, "CSV" formats vary greatly in this choice of separator character. In particular, in locales where the comma is used as a decimal separator, semicolon, TAB, or other characters are used instead.

ryu1kn commented 6 years ago

Changed List separator from , to ; and Excel used ; for field separator when I saved data into a CSV file. Fields containing , are not wrapped with double-quotes, but fields containing ; are now wrapped instead.

list-separator-settings

ryu1kn commented 6 years ago

Merged PR #8 , thanks @HKskn !!

ryu1kn commented 6 years ago

Published as v1.1.0. Now semicolon is also treated as a valid field separator. I'll create a separate card for TSV.