postgresql 带有文字定界符的 Postgres COPY 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26160816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgres COPY command with literal delimiter
提问by toanong
I was trying to import a CSV file into a PostgreSQLtable using the COPYcommand. The delimiter of the CSVfile is comma (,). However, there's also a text field with a comma in the value. For example:
我试图使用COPY命令将 CSV 文件导入到PostgreSQL表中。CSV文件的分隔符是逗号 (,)。但是,还有一个值中带有逗号的文本字段。例如:
COPY schema.table from '/folder/foo.csv' delimiter ',' CSV header
Here's the content of the foo.csv
file:
这是foo.csv
文件的内容:
Name,Description,Age
John,Male\,Tall,30
How to distinguish between the literal comma and the delimiter?
如何区分文字逗号和分隔符?
Thanks for your help.
谢谢你的帮助。
回答by Clodoaldo Neto
To have the \
to be recognized as a escape character it is necessary to use the text format
要将\
其识别为转义字符,必须使用文本格式
COPY schema.table from '/folder/foo.csv' delimiter ',' TEXT
But then it is also necessary to delete the first line as the HEADER
option is only valid for the CSV
format.
但随后也有必要删除第一行,因为该HEADER
选项仅对CSV
格式有效。