Can you encode CR/LF in into CSV files?
Asked Answered
N

4

111

Is it possible/legal to somehow encode CR/LF characters into a CSV file?

(as part of a CSV standard?)

If so how should I encode CR/LF?

Nonproductive answered 19/2, 2009 at 16:18 Comment(0)
R
140

Yes, you need to wrap in quotes:

"some value
over two lines",some other value

From this document, which is the generally-accepted CSV standard:

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes

P.S For fields with double quotes inside, you should also escape double quotes with two quotes, e.g

"Samsung U600 24"""
Reductase answered 19/2, 2009 at 16:20 Comment(9)
Isnt the generally-accepted format RFC 4180 ? en.wikipedia.org/wiki/Comma-separated_valuesDiaphone
The wikipedia article also says "... (however, many CSV implementations do not support embedded line breaks)."Harness
@Diaphone RFC 4180 says the same thing here. Page 2, item 6.Expository
@Expository indeed, I'm just trying encourage OP to send visitors to the official RFC, instead of claiming this other site is the 'generally-accepted CSV standard'Diaphone
even after adding double-quotes, the data gets inserted in new cell.Stephi
If the double quotes (") shows up and the line-breaks still cause trouble, look for spaces! There should be no space between the coma (,) ending the previous cell and the double quote (") starting the multiline text.Flap
This simply is not working, not even when re-importing a CSV with multi-line cells exported as CSV right from Excel itself. Excel from MS 365 does not import the file correctly, not even its own variant with CRLF line feeds vs. LF for multi-line cells.Rake
Update: It is working, if (and only if) the CSV uses semicolon (not comma) as a column separator and - very important - the file is opened via double-click, not via Excel's "open file" dialogue. How crazy is that?!Rake
This works importing to Jira.Penury
E
20

the most common variant of csv out there which is the excel compatible one will allow embedded newlines so long as the field is surrounded by double quotes.

foo,bar,"blah blah
more blah blah",baz

or

foo,bar,"blah blah
more blah blah"

or

"blah blah
more blah blah",baz

are all valid. This mechanism also allows for embedded commas.

Using quotes around textual fields without embedded new lines (or commas) is fine too. If the text itself contains a double quote then mechanism to escape it is to put two together, for example.

foo,bar,"this person said ""blah blah 
more blah blah""",baz

Writing a csv reader that handles this correctly can be tricky (especially if you are relying on regular expressions).

Enceinte answered 19/2, 2009 at 16:26 Comment(2)
what if I want to embed a quote?Calyx
Can't confirm this. Importing the data with any newline character in a quoted field breaks the rest of the cells into a new spreadsheet row for me in Office 365 ProPlus Excel v1808. Encoded with Windows-1250.Apocarp
N
12

Mention has been made here of a standard for CSV. I'd be interested to know more about this - the only standards I'm aware of are

Normy answered 19/2, 2009 at 16:43 Comment(3)
yes, the RFC you link to is the definitive standard. It mentions putting CRLF inside double quotes to escape it. Unfortunately, your point about whatever excel accepts is valid... yet another case of MS trying to subvert standards.Wainscoting
That RFC was created in 2005! Excel has supported CsV for quite a lot longer then that...Enceinte
ShuggyCoUk that was the last update, it was created long before that : en.wikipedia.org/wiki/Comma-separated_valuesDiaphone
W
1

I don't think it's part of the standard (if there even is one), but you could use standard C-style escaping, i.e. encode \r\n.

Keep in mind, however, that if you do that you should also encode the escape character -- i.e. \\ yields \ after decoding.

Weikert answered 19/2, 2009 at 16:20 Comment(3)
csv does not use C-style escapingNormy
some csv apps support this form. csv is a regrettably poorly defined standard. Talking Excel csv is (pragmatically) best though which this is notEnceinte
IETF RFC 4180 :)Alluring

© 2022 - 2024 — McMap. All rights reserved.