0

Let's say i have the the following:

var csv = [['name', 'comment'], ['foo', 'hello world, bar']];

When I try creating a csv string and opening that csv string in Excel, it becomes something like this

name | comment     |
-------------------------
foo  | hello world | bar

How would I get around this?

Lawrence Vo
  • 177
  • 2
  • 11

1 Answers1

0

In Excel you can select which delmitiers you want to use.
Just use a semicolon or any other char that you dont use inside your text.

Code Spirit
  • 3,992
  • 4
  • 23
  • 34
  • looking at the following wikipedia pge, maybe that's valid as per the RFC 4180 definition of csv(which allows other delimiters other than commas), but not the original definition https://en.wikipedia.org/wiki/Comma-separated_values If you look at the first few lines of that wikipedia page it says "In computing, a comma-separated values (CSV) file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format". – barlop Sep 30 '16 at 18:02
  • But further down under specification it says: "3. with the records divided into fields separated by delimiters (typically a single reserved character such as comma, semicolon, or tab; sometimes the delimiter may include optional spaces),". So even if he wants to use commas as a seperator, his CSV wont be valid because then his comma is a reserved character and cannot be used inside a data field. – Code Spirit Sep 30 '16 at 18:31
  • well, I was making the point that the original definition said comma only (i.e. no semicolon as delimiter), but the rfc proposed spec allows other delimiters. As to whether a comma can be in the data, it can't as per the rfc, but in the original definition, it says "The basic idea of separating fields with a comma is clear, but that idea gets complicated when the field data may also contain commas or even embedded line-breaks. CSV implementations may not handle such field data, or they may use quotation marks to surround the field(cont)..... – barlop Sep 30 '16 at 22:57
  • (cont)Quotation does not solve everything: some fields may need embedded quotation marks, so a CSV implementation may include escape characters or escape sequences. In addition, the term "CSV" also denotes some closely related delimiter-separated formats that use different field delimiters. " – barlop Sep 30 '16 at 22:58
  • In excel if you put a comma in a cell and save as csv then it puts quotation marks around the whole cell contents. I just tried to break it by putting a quotation mark in a cell at the end of the cell, and I think it dealt with it, it stored it as `"bob"""` so maybe it put quotation marks around the whole cell content and escaped a quotation mark within! That'd probably be a good question for puzzle stackexchange, how can you break it. Maybe two quotation marks would break it. Breaking it would mean causing an error or getting different data read in than was entered. – barlop Sep 30 '16 at 23:06