Postgresql COPY CSV ERROR:最后一个预期列之后的额外数据

Posted

技术标签:

【中文标题】Postgresql COPY CSV ERROR:最后一个预期列之后的额外数据【英文标题】:Postgresql COPY CSV ERROR: extra data after last expected column 【发布时间】:2013-11-30 14:54:37 【问题描述】:

我正在尝试从http://www.unitedstateszipcodes.org/zip-code-database 导入数据。数据子集如下所示:

"zip","type","primary_city","acceptable_cities","unacceptable_cities","state","county","timezone","area_codes","latitude","longitude","world_reg$
"00501","UNIQUE","Holtsville",,"I R S Service Center","NY","Suffolk County","America/New_York","631","40.81","-73.04","NA","US","0","384",
"00544","UNIQUE","Holtsville",,"Irs Service Center","NY","Suffolk County","America/New_York","631","40.81","-73.04","NA","US","0","0"

我运行的 postgresql 命令是这样的:

copy development.zip_codes FROM '/tmp/zip_code_database.csv' WITH DELIMITER ',' CSV HEADER;

结果是这样的:

ERROR: extra data after last expected column
SQL state: 22P04
Context: COPY zip_codes, line 2: ""00501","UNIQUE","Holtsville",,"I R S Service Center","NY","Suffolk County","America/New_York","631"..."

我在导入时做错了什么?

【问题讨论】:

【参考方案1】:

在这里工作就像一个魅力......

DROP TABLE zipcodes CASCADE;
CREATE TABLE zipcodes
        ( id serial NOT NULL PRIMARY KEY
        , zzip varchar NOT NULL UNIQUE
        , ztype varchar
        , primary_city varchar
        , acceptable_cities varchar
        , unacceptable_cities varchar
        , state varchar
        , county varchar
        , ztimezone varchar
        , area_codes varchar
        , latitude varchar
        , longitude varchar
        , world_region varchar
        , country varchar
        , decommissioned varchar
        , estimated_population varchar
        , notes varchar
        ); 

COPY zipcodes (zzip,ztype,primary_city
     , acceptable_cities,unacceptable_cities
     , state,county,ztimezone,area_codes 
     , latitude,longitude,world_region,country
     , decommissioned,estimated_population,notes )
FROM '/tmp/zip_code_database.csv'
        WITH CSV HEADER delimiter ','
        ;

结果:

DROP TABLE
CREATE TABLE
COPY 42522

(也许 OP 有 CR/CRLF 问题?)

【讨论】:

列内容似乎被转移到了标题中,用不可接受的状态列说明;您可能需要稍微调整列... 注意:我需要明确的名单,因为我已经添加了序列号id 列。显式列表还可以对项目进行洗牌。 WRT?这意味着什么?在我的示例中,我还尝试将邮政编码作为主键,但您使用的是序列号。 糟糕,我搞砸了:我也无意中读到了id 系列。已更新

以上是关于Postgresql COPY CSV ERROR:最后一个预期列之后的额外数据的主要内容,如果未能解决你的问题,请参考以下文章

postgresql数据库 copy命令导入表不成功

PostgreSQL COPY 附加页眉 - csv 中的页脚行

带有两个 NULL 字符串的 PostgreSQL COPY CSV

postgresql的copy命令如何将csv中的引号转换成null插入?

将压缩的 CSV 文件导入 PostgreSQL

我可以从带有标题的csv文件中自动在PostgreSQL中创建一个表吗?