规范:
Linux Mint 18.x
Posgres 10.x
pgadmin3
我尝试将 TSV 加载到 postgres 中,但不断收到回车错误。文件中用“\N”表示NULL。
我尝试过使用 \copy
并使用 pgadmin3 中的导入对话框。在 pgadmin3 中,我尝试省略文件格式,并尝试将其设置为 UTF8 。错误仍然存在。
使用的初始命令:
\copy table FROM PROGRAM 'tail -n +2 /home/super/Downloads/folder/myfile.tsv'
ERROR; literal carriage return found in data
HINT: use "\r" to represent carriage return.
我一直在使用 sed 创建不同版本的文件来替换我认为可能导致错误的内容:
sed 's/\n/\r/g myfile.tsv > newfile1.tsv'
sed 's/\\n/\r/g myfile.tsv > newfile2.tsv'
sed 's/\\n//g myfile.tsv > newfile3.tsv'
我还尝试了以下方法(非按时间顺序)
sed 's/\r\n/\r/g' new.tsv
sed 's/\\N/NULL/g' new.tsv
sed 's/\\//'
sed 's/\\N/\r/'
sed -n 's/\n/\r/'
sed -n 's/\\n/\r/'
sed 's/\N/\r/'
sed 's/\\N/NULL/'
sed 's/\N/NULL/'
sed 's/\r//'
sed 's/\N/NULL/'
sed 's/\\N/NULL/'
sed 's/\\//'
sed 's/\N/\r/g' new.tsv
sed 's/\N/NULL/g' new.tsv
sed 's/\N/NULL/g' new.tsv
但这些都不起作用。当我使用 LibreOffice 的预览对话框查看时,它似乎可以滚动内容并将其格式化为表格。
我看过this关于文字换行错误和 this question about using copy 的问题.
我不明白插入错误的“字节”是什么意思。
数据预览:/image/HvOTz.jpg
更新:运行sed 's/\r/CR was here/g' myfile.tsv | grep 'CR was here'
返回两个结果
最佳答案
这个问题可以通过使用以下方法解决:
sed 's/\r//\\r/g' myfile.tsv > myfile_copy.tsv
显然需要转义两次。
@Abelisto 指出了此评论中出现的错误:
Ok, there is relatively simple way to reproduce the error:
echo -e 'aaa\nbbb\rccc' > foo.tsv ; psql -c 'create table foo(x text)' -c '\copy foo from foo.tsv' -c 'drop table foo;'
(be careful if you already have valuable foo table in your DB) Lets try to find how to fix it...
也有帮助:
echo -e 'aaa\nbbb\rccc' > foo.tsv ; sed -i 's/\r/\\r/g' foo.tsv ;
psql -c 'create table foo(x text)' -c "\copy foo from foo.tsv" -c
'table foo' -c 'drop table foo;'
fixed the error. I have no ideas why \r sequence does not translated to CR by copy command as mentioned in the documentation. But it is yet another question. – Abelisto
关于sql - Linux 上的 Postgres 文字回车错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144941/