简体   繁体   English

UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe4'

[英]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 我已经试过了

  1. x.encode("ascii", "ignore")
  2. x.encode("utf-8")
  3. 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.

相关问题 UnicodeEncodeError:'ascii'编解码器无法编码字符u'\\ xe9' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe9' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:'ascii'编解码器无法编码位置17710中的字符u'\ xe7':序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128) UnicodeEncodeError: 'ascii' codec can't encode character u'\\xe9' in position 54: ordinal not in range(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 54: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器无法编码字符u'\\ xa3' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' UnicodeEncodeError: 'ascii' 编解码器无法编码字符 '\’' - UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xef' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xef' 'ascii'编解码器无法编码字符u'\\ xe9' - 'ascii' codec can't encode character u'\xe9' Python eyed3 UnicodeEncodeError:'ascii'编解码器无法在位置17编码字符u'\\ xe9':序数不在范围内(128) - Python eyed3 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 17: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器不能编码字符[...] - UnicodeEncodeError: 'ascii' codec can't encode character […]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM