Can you encode CR/LF in into CSV files?

前端 未结 4 1553
北海茫月
北海茫月 2020-11-27 15:51

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?

4条回答
  •  青春惊慌失措
    2020-11-27 16:10

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

提交回复
热议问题