Postgres CSV复制声明

时间:2013-12-29 13:43:07

标签: postgresql parsing csv upload

大家好我有问题更好说错误问题。 postgres中的MY函数看起来像:

CREATE TEMP TABLE aljazeera

(
    date character varying,
    channel_name character varying,
    date1 character varying,
    start character varying,
    program character varying,
    sub character varying,
    epis_title character varying
    ) ;

COPY aljazeera FROM '/opt/transcode/data/epg/output/CSV/ALJAZEERA/pre_g_1.csv' WITH (FORMAT CSV, DELIMITER ',', QUOTE '"');

我的文件看起来像是:

http://www.speedyshare.com/bNcjM/pre-g-1.csv

警告:可能是恶意软件,在下载链接上肯定是令人讨厌的软件。只使用顶部链接Download: pre-g-1.csv)。

当我尝试上传到表时,它会说错误:

ERROR:  unterminated CSV quoted field
CONTEXT:  COPY aljazeera, line 175: "2013-12-24,02:00:00","KRAJ PROGRAMA",,,,
2013-12-24,07:30:00,"Sportski magazin  (R)",,"Sportski doga..."

我不知道问题出在哪里。对此问题的任何建议。

1 个答案:

答案 0 :(得分:2)

一旦我最终下载了没有那个讨厌的下载管理器的文件,我就可以重现错误:

craig=> \copy aljazeera FROM 'pre_g_1.csv' WITH (FORMAT CSV, DELIMITER ',', QUOTE '"')
ERROR:  unterminated CSV quoted field
CONTEXT:  COPY aljazeera, line 175: "2013-12-24,02:00:00","KRAJ PROGRAMA",,,,
2013-12-24,07:30:00,"Sportski magazin  (R)",,"Sportski doga..."

该错误实际上并不在第175行。这是因为某些先前的行具有不平衡的报价。通过二分搜索,很容易将其缩小到第26行:

2013-12-24,02:00:00","KRAJ PROGRAMA",,,,

我相信你可以看到问题所在。你在约会时有一个流浪的报价。

顺便说一句,如果您需要链接到文本块,可以使用http://gist.github.com/http://pastebin.com/http://pastebin.ca/等。(对于PostgreSQL查询计划http://explain.depesz.com/是最好)。快节奏的事情很讨厌。