Postgresql:带有转义换行符的CSV导出 [英] Postgresql: CSV export with escaped linebreaks
问题描述
我使用此处发布的所有说明从Postgresql数据库中导出了一些数据:将PostgreSQL的PL / pgSQL输出保存到CSV文件中
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
但是某些导出的字段包含换行符(换行符),所以我得到了CSV像这样的文件:
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)?
推荐答案
如果CSV支持换行符,
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"
它将被视为1行数据,分布在2行3字段中,
it will be taken as 1 line of data spread on 2 lines with 3 fields and just work.
另一方面,如果没有双引号,则它是无效的CSV文件(当广告3个字段时)。
On the other hand, without the double quotes, it's an invalid CSV file (when it advertises 3 fields).
尽管没有CSV格式的正式规范,但您可以查看 RFC 4180 了解通常适用的规则。
Although there's no formal specification for the CSV format, you may look at RFC 4180 for the rules that generally apply.
这篇关于Postgresql:带有转义换行符的CSV导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!