本文介绍了UnicodeEncodeError:'ascii'编解码器不能编码字符u'\xe4'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我永久得到以下错误:

  UnicodeEncodeError:'ascii'编解码器无法编码字符u'\xe4 '在位置27:序数不在范围(128)

我已经尝试过


  1. x.encode(ascii,ignore)

  2. x.encode(utf-8)

  3. x.decode(utf-8 )

然而,没有任何作用。

解决方案

你必须在源码中发现这个字符的编码。



我猜这是ISO-8859-1 (欧洲语言),在这种情况下它是ä,但你应该检查。也可以是西里尔语或希腊语。



请参阅,获取此编码中字符的完整列表。



使用这个信息,你可以问Python转换它:



在Python 2.7

 >>> s ='\xe4'
>>> 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
pre>

String t 内部编码为Python中的ISO-8859-1。字符串 u 内部编码为UTF-8,该字符在UTF-8中占用2个字节。请注意, print 指令知道如何显示这些不同的编码。


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. 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. String u is internally encoded in UTF-8, and that character takes 2 bytes in UTF-8. Notice also that the print instruction "knows" how to display these different encodings.

这篇关于UnicodeEncodeError:'ascii'编解码器不能编码字符u'\xe4'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:00