如何在执行psql copy命令时保留新行字符

时间:2018-06-13 11:41:03

标签: postgresql csv-import postgresql-copy

我的csv文件中有以下内容(包含3列):

141413,"\"'/x=/></script></title><x><x/","Mountain View, CA\"'/x=/></script></title><x><x/"

148443,"CLICK LINK BELOW TO ENTER^^^^^^^^^^^^^^","model\
\
xxx lipsum as it is\
\
100 sometimes unknown\
\
travel evening market\
"

当我使用以下命令在mysql中导入上面提到的csv时,它将反斜杠()视为新行;这是预期的行为。

LOAD DATA INFILE '1.csv' INTO TABLE users FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n';

MYSQL Output

但是当我尝试使用copy命令导入到psql时,它会将\视为普通字符。

copy users from '1.csv' WITH (FORMAT csv, DELIMITER ',', ENCODING 'utf8', NULL "\N", QUOTE E'\"', ESCAPE '\');

postgres Output

1 个答案:

答案 0 :(得分:0)

在导入CSV文件之前尝试解析这些\,例如使用perl -pesed以及psql中的STDIN

$ cat 1.csv | perl -pe 's/\\\n/\n/g' | psql testdb -c "COPY users FROM STDIN WITH (FORMAT csv, DELIMITER ',', ENCODING 'utf8', NULL "\N", QUOTE E'\"', ESCAPE '\');"

这是导入后的样子:

testdb=# select * from users;
   id   |                 company                 |                    location                     
--------+-----------------------------------------+-------------------------------------------------
 141413 | "'/x=/></script></title><x><x/          | Mountain View, CA"'/x=/></script></title><x><x/
 148443 | CLICK LINK BELOW TO ENTER^^^^^^^^^^^^^^ | model                                          +
        |                                         |                                                +
        |                                         | xxx lipsum as it is                            +
        |                                         |                                                +
        |                                         | 100 sometimes unknown                          +
        |                                         |                                                +
        |                                         | travel evening market                          +
        |                                         | 
(2 Zeilen)