\copy 字段内带双引号的 CSV 到 table
\copy CSV with double quotes inside field to table
CSV 文件如下所示:
c1,c2,c3,c4,c5,c6
"153";"0";"5";"39264";"hey";"4 spaces; not"can;hen" thanks!"
最后一个字段在字符串中包含双引号和分号。当我尝试像这样将文件复制到 table 时:
psql -U user -d postgres -c "\copy file from 'file.csv' with delimiter as ';' csv header;"
我收到错误 ERROR: extra data after last expected column.
双引号和分号只存在于最后一列。
如何更改 psql
命令以便成功复制此 CSV table?
您不能更改命令,您必须更改文件,因为该文件在语法上不是正确的 CSV。
您必须将不是字段分隔符的双引号加倍:
"153";"0";"5";"39264";"hey";"4 spaces; not""can;hen"" thanks!"
CSV 文件如下所示:
c1,c2,c3,c4,c5,c6
"153";"0";"5";"39264";"hey";"4 spaces; not"can;hen" thanks!"
最后一个字段在字符串中包含双引号和分号。当我尝试像这样将文件复制到 table 时:
psql -U user -d postgres -c "\copy file from 'file.csv' with delimiter as ';' csv header;"
我收到错误 ERROR: extra data after last expected column.
双引号和分号只存在于最后一列。
如何更改 psql
命令以便成功复制此 CSV table?
您不能更改命令,您必须更改文件,因为该文件在语法上不是正确的 CSV。
您必须将不是字段分隔符的双引号加倍:
"153";"0";"5";"39264";"hey";"4 spaces; not""can;hen"" thanks!"