将postgres表导出为csv会得到UnicodeEncodeError

时间:2019-08-02 10:40:56

标签: python postgresql csv unicode psycopg2

我有上面的代码,它将Postgres表复制到csv文件。

cur = db.cursor()
q = "COPY (select * from assessments) TO STDOUT WITH CSV DELIMITER ';' ENCODING 'UTF-8'"
with open("table.csv", "w") as file:
    cur.copy_expert(q, file)
cur.close()

尽管如此,当我运行它时,出现以下错误:

Traceback (most recent call last):
  File "postgrestocsv.py", line 31, in <module>
    cur.copy_expert(q, file)
  File "C:\Users\andri\Anaconda3\lib\encodings\cp1253.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u202f' in position 343: character maps to <undefined>

有人可以帮助我了解问题所在吗?感谢您能提供的任何帮助

0 个答案:

没有答案