问题描述
我永久得到以下错误:
UnicodeEncodeError:'ascii'编解码器无法编码字符u'\xe4 '在位置27:序数不在范围(128)
我已经尝试过
-
x.encode(ascii,ignore)
-
x.encode(utf-8)
-
x.decode(utf-8 )
然而,没有任何作用。
你必须在源码中发现这个字符的编码。
我猜这是ISO-8859-1 (欧洲语言),在这种情况下它是ä,但你应该检查。也可以是西里尔语或希腊语。
请参阅,获取此编码中字符的完整列表。
使用这个信息,你可以问Python转换它:
在Python 2.7
>>> s ='\xe4'
pre>
>>> t = s.decode('iso-8859-1')
>>> print t
ä
>>>对于c:
... print ord(c)
...
228
>>> u = t.encode('utf-8')
>>>>打印u
ä
>>>对于c(以字节为单位):
... print ord(c)
...
195
164
String
t
内部编码为Python中的ISO-8859-1。字符串u
内部编码为UTF-8,该字符在UTF-8中占用2个字节。请注意,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. 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.
Using this information, you can ask Python to convert it :
In 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. Stringu
is internally encoded in UTF-8, and that character takes 2 bytes in UTF-8. Notice also that the这篇关于UnicodeEncodeError:'ascii'编解码器不能编码字符u'\xe4'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!