python编码到UTF-8,给我额外的字符
我有一大堆字符串必须插入到sqlite数据库中,但其中许多都有一些奇怪的字符,等等。现在我可以按我想要的方式格式化它,但如果我尝试将其写入一个文件,我会得到python编码到UTF-8,给我额外的字符,python,encoding,utf-8,Python,Encoding,Utf 8,我有一大堆字符串必须插入到sqlite数据库中,但其中许多都有一些奇怪的字符,等等。现在我可以按我想要的方式格式化它,但如果我尝试将其写入一个文件,我会得到 UnicodeEncodeError: 'charmap' codec can't encode characters in position 82-85: character maps to <undefined> 请注意,Insert只是一个列值表。现在,我可以通过在写入之前编码到utf-8来修复该错误: statemen
UnicodeEncodeError: 'charmap' codec can't encode characters in position 82-85: character maps to <undefined>
请注意,Insert只是一个列值表。现在,我可以通过在写入之前编码到utf-8来修复该错误:
statement.encode('utf-8', 'ignore')
但是我写的文本文件有很多额外的字符,比如文本前面的b和到处的“\”,我想这是因为它被转换成字节而不是字符串
编码前:
'Insert into table values ('Tue Nov 05 00:00:04 +0000 2013'
b'Insert into table values (\'Tue Nov 05 00:00:04 +0000 2013\'
编码后:
'Insert into table values ('Tue Nov 05 00:00:04 +0000 2013'
b'Insert into table values (\'Tue Nov 05 00:00:04 +0000 2013\'
我假设我在这里做了某种解码/编码错误,但没有任何运气
b
在python2中表示它是str
,在python3中表示它是bytes
。Insert
中项目的类型是什么?它是一个字符串数组。