too*_*oom 17 python encoding ascii
我永久收到以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 27: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我已经试过了
x.encode("ascii", "ignore")x.encode("utf-8")x.decode("utf-8")然而,没有任何作用.
小智 11
您必须在源处发现此字符的编码.
我想这是ISO-8859-1(欧洲语言),在这种情况下它是"ä",但你应该检查.它也可以是西里尔文或希腊文.
有关此编码中的完整字符列表,请参见http://en.wikipedia.org/wiki/ISO/IEC_8859-1.
使用此信息,您可以要求Python进行转换:
在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
Run Code Online (Sandbox Code Playgroud)
String t在Python中以ISO-8859-1内部编码.字符串u在内部以UTF-8编码,该字符在UTF-8中占用2个字节.另请注意,print指令"知道"如何显示这些不同的编码.