[英]Encoding and Decoding UTF-8 and latin1
I'm studying someone's code for processing data, and got errors on this line: 我正在研究某人的代码来处理数据,并在此行出现错误:
chars_sst_mangled = ['à', 'á', 'â', 'ã', 'æ', 'ç', 'è', 'é', 'í',
'í', 'ï', 'ñ', 'ó', 'ô', 'ö', 'û', 'ü']
sentence_fixups = [(char.encode('utf-8').decode('latin1'), char) for char in chars_sst_mangled]
The error message is 错误消息是
"UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)"
I wonder what's the problem here, and how to fix it? 我想知道这里有什么问题,以及如何解决?
The code is broken. 代码已损坏。
The specific error indicates that you are trying to run Python 3 code using python2 executable: 特定错误表明您正在尝试使用python2可执行文件运行Python 3代码:
>>> 'à'.encode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
'à'
is a bytestring on Python 2 and therefore calling .encode()
method requires to decode the bytestring into Unicode first. 'à'
是Python 2上的字节.encode()
,因此调用.encode()
方法需要先将字节.encode()
解码为Unicode。 It is done using sys.getdefaultencoding()
that is 'ascii'
in Python 2 that triggers the UnicodeDecodeError
. 它是使用进行
sys.getdefaultencoding()
即'ascii'
在Python 2触发UnicodeDecodeError
。
The correct way would be to drop bogus char.encode('utf-8').decode('latin1')
conversion and use Unicode literals instead: 正确的方法是删除伪造的
char.encode('utf-8').decode('latin1')
转换并改用Unicode文字:
# -*- coding: utf-8 -*-
at the top so that non-ascii characters in string literals hardcoded in the source would be interpreted correctly # -*- coding: utf-8 -*-
放在顶部,以便将在源中硬编码的字符串文字中的非ascii字符正确解释 from __future__ import unicode_literals
so that 'à'
would create a Unicode string even on Python 2. from __future__ import unicode_literals
添加from __future__ import unicode_literals
这样即使在Python 2上, 'à'
也可以创建Unicode字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.