如何从混合编码文件导入 PostgreSQL table

How to import from a mixed-encoding file to a PostgreSQL table

我有一个 30 GB 的文本文件。该文件的编码是 UTF8,但它还包含一些 Windows-1252 个字符。因此,当我尝试导入时,出现以下错误:

ERROR:  invalid byte sequence for encoding "UTF8": 0x9b

我该如何解决这个问题?

文件已经是UTF8格式,当我运行这个文件的'file'命令时它说编码是UTF8。但它也包含一些非 UTF8 字节序列。例如,当我 运行 \copy 命令一段时间后,它给出了该行的上述错误:

0B012234    Basic study of <img src="/fulltext-image.asp?format=htmlnonpaginated&src=323K744431152658_html3_2    basic study of img src fulltext image asp format htmlnonpaginated src 323k744431152658_html 233_2   1975        Semigroup Forum semigroup forum 04861B53        19555

问题是由反斜杠 (\) 引起的。
使用不将反斜杠视为特殊字符的 CSV 格式,例如-

\copy t from myfile.txt with csv quote E'\x1' delimiter E'\x2'