1

我的 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 输出

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

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

postgres 输出

4

1 回答 1

0

\在导入 CSV 文件之前尝试解析这些,例如使用perl -peorsedSTDINfrom psql

$ 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)
于 2018-06-13T12:35:25.173 回答