我试图将CSV文件导入PostgreSQL,但我遇到了特殊字符的问题。
我使用以下命令
./psql -d data -U postgres -c "copy users from 'users.csv' delimiter E'\t' quote '~' csv"
它可以正常工作,直到遇到带有'〜'我用它作为引用值,不破坏现有的引号和引号等等。
如何在csv文件中转义此字符' Person~name'这样它就会导入为人物名称'
答案 0 :(得分:2)
CSV规则列在https://www.ietf.org/rfc/rfc4180.txt
中将引号字符嵌入字符串中:
- 醇>
如果使用双引号括起字段,则使用双引号 出现在一个字段内必须通过前面的方式进行转义 另一个双引号。例如:
“AAA”, “B”, “BB”, “CCC”
在你的情况下,用tilde替换双引号,因为你已经选择了分隔符。
示例:
test=> create table copytest(t text); CREATE TABLE test=> \copy copytest from stdin delimiter E'\t' quote '~' csv Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> ~foo~~bar~ >> \. test=> select * from copytest; t --------- foo~bar