Can you encode CR/LF in into CSV files?

Csv

Csv Problem Overview


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?

Csv Solutions


Solution 1 - Csv

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

Solution 2 - Csv

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).

Solution 3 - Csv

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

Solution 4 - Csv

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTore NesteniusView Question on Stackoverflow
Solution 1 - CsvKent BoogaartView Answer on Stackoverflow
Solution 2 - CsvShuggyCoUkView Answer on Stackoverflow
Solution 3 - CsvanonView Answer on Stackoverflow
Solution 4 - CsvRandolphoView Answer on Stackoverflow