[英]UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4'
I permanently get the following error: 我永久收到以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 27: ordinal not in range(128)
I already tried 我已经试过了
x.encode("ascii", "ignore")
x.encode("utf-8")
x.decode("utf-8")
However, nothing works. 然而,没有任何作用。
You have to discover in which encoding is this character at the source. 您必须在源处发现此字符的编码。
I guess this is ISO-8859-1 (european languages), in which case it's "ä", but you should check. 我想这是ISO-8859-1(欧洲语言),在这种情况下它是“ä”,但你应该检查。 It could also be cyrillic or greek.
它也可以是西里尔文或希腊文。
See http://en.wikipedia.org/wiki/ISO/IEC_8859-1 for a complete list of characters in this encoding. 有关此编码中的完整字符列表,请参见http://en.wikipedia.org/wiki/ISO/IEC_8859-1 。
Using this information, you can ask Python to convert it : 使用此信息,您可以要求Python进行转换:
In Python 2.7 在Python 2.7中
>>> s = '\xe4'
>>> t = s.decode('iso-8859-1')
>>> print t
ä
>>> for c in t:
... print ord(c)
...
228
>>> u = t.encode('utf-8')
>>> print u
ä
>>> for c in bytes(u):
... print ord(c)
...
195
164
String t
is internally encoded in ISO-8859-1 in Python. 字符串
t
在Python中以ISO-8859-1内部编码。 String u
is internally encoded in UTF-8, and that character takes 2 bytes in UTF-8. 字符串
u
在内部以UTF-8编码,该字符在UTF-8中占用2个字节。 Notice also that the print
instruction "knows" how to display these different encodings. 另请注意,
print
指令“知道”如何显示这些不同的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.