导入到Postgres时,从CSV中删除封闭的双括号 [英] Remove enclosing double brackets from CSV when importing to postgres

查看:119
本文介绍了导入到Postgres时,从CSV中删除封闭的双括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3列的CSV;文字,整数,文字。当我导入这些数据时,所有数据都放在双括号中。我不确定为什么。是否可以使用COPY在导入时删除这些文件?还是我应该作为文本导入,然后使用 select replace 删除它们?

I have a CSV with 3 columns ; text, integer, text. When i import these all the data is enclosed in double brackets. I'm not sure why. Is there away to remove these on import using COPY? Or should i import as text and then remove them using select replace ?

修改;复制代码

从'file.csv'复制带有分隔符'tavle',CSV编码为'windows-1251'

原始csv数据示例;

Sample of raw csv data;

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


推荐答案

如果目标表和csv文件具有同样的结构,我看不到这是怎么可能的。请按照以下示例操作:

If your target table and csv file have the same structure, I fail to see how it is possible. Follow this example:

考虑以下CSV文件...

Considering the following CSV file ...

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)

这篇关于导入到Postgres时,从CSV中删除封闭的双括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆