解决psycopg2.DataError: invalid byte sequence for encoding "UTF8": 0x00

出错场景:

在使用psycopg2的copy_expert插入数据时,遇到了这个错误。

psycopg2.DataError: invalid byte sequence for encoding "UTF8": 0x00
HINT:  This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".

错误原因:

“0x00”在python中代表“\x00”字符,它是单个字符(与“\0”是同一个对象),如果用print(‘\x00’)输出,是不可见的,可以用repr(‘\x00’)输出查看

In [1]: print('\x00')


In [2]: repr('\x00')
Out[2]: "'\\x00'"

In [3]: with open('0x00.csv','w') as w:
   ...:     w.write('\x00')
   ...:     

用vim查看含有该字符的文件,会显示为“^@”

^@

解决办法:

将该字符串移除,使用replace方法即可

content='\x00'
content=content.replace('\x00','')

你可能感兴趣的:(数据库,Python)