导入到postgres时,从CSV中删除封闭的双括号

时间:2018-04-30 12:15:33

标签: postgresql csv

我有一个包含3列的CSV;文本,整数,文本。当我导入这些时,所有数据都用双括号括起来。我不知道为什么。是否有使用COPY导入时删除这些?或者我应该导入为文本,然后使用select replace删除它们?

编辑;复制代码

copy tavle from 'file.csv' with delimiter ',' CSV encoding 'windows-1251'

原始csv数据样本;

"TY373543","4510069420","A"
"ND4368","4510093370","A"
"TY152401","4510093370","A"
"TY384054","4510069421","A"

1 个答案:

答案 0 :(得分:2)

如果您的目标表和csv文件具有相同的结构,我无法看到它是如何可能的。请遵循以下示例:

考虑以下CSV文件...

x, y, z
"TY373543","4510069420","A"
"ND4368","4510093370","A"
"TY152401","4510093370","A"
"TY384054","4510069421","A"

和下表结构...

CREATE TABLE t (a text, b bigint, c text);

只需执行此命令:

COPY t FROM '/tmp/file.csv' CSV HEADER;

以下是您的数据:

   SELECT * FROM t;
    a     |     b      | c 
----------+------------+---
 TY373543 | 4510069420 | A
 ND4368   | 4510093370 | A
 TY152401 | 4510093370 | A
 TY384054 | 4510069421 | A
(4 Zeilen)