Postgresql: CSV export with escaped linebreaks

我的未来我决定 提交于 2019-12-01 19:56:35

问题


I exported some data from a postgresql database using (all) the instruction(s) posted here: Save PL/pgSQL output from PostgreSQL to a CSV file

But some exported fields contains newlines (linebreaks), so I got a CSV file like:

header1;header2;header3
foobar;some value;other value
just another value;f*** value;value with
newline
nextvalue;nextvalue2;nextvalue3

How can I escape (or ignore) theese newline character(s)?


回答1:


Line breaks are supported in CSV if the fields that contain them are enclosed in double quotes.

So if you had this in the middle of the file:

just another value;f*** value;"value with
newline"

it will be taken as 1 line of data spread on 2 lines with 3 fields and just work.

On the other hand, without the double quotes, it's an invalid CSV file (when it advertises 3 fields).

Although there's no formal specification for the CSV format, you may look at RFC 4180 for the rules that generally apply.



来源:https://stackoverflow.com/questions/18513331/postgresql-csv-export-with-escaped-linebreaks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!