UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-17 15:20:42

问题


I'm having troubles in encoding characters in utf-8. I'm using Django, and I get this error when I tried to send an Android notification with non-plain text. I tried to find where the source of the error and I managed to figure out that the source of the error is not in my project.

In python shell, I type:

'ç'.encode('utf8')

and I get this error:

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)

I get the same errors with:

'á'.encode('utf-8')
unicode('ç')
'ç'.encode('utf-8','ignore')

I get errors with smart_text, force_text and smart_bytes too.

Is that a problem with Python, my OS, or another thing?

I'm running Python 2.6.6 on a Red Hat version 4.4.7-3


回答1:


You're trying to encode / decode strings, not Unicode strings. The following statements do work:

u'ç'.encode('utf8')
u'á'.encode('utf-8')
unicode(u'ç')
u'ç'.encode('utf-8','ignore')



回答2:


Use u'...', without the u prefix it is byte-string not a unicode string.:

>>> u'ç'.encode('utf8')
'\xc3\xa7'


来源:https://stackoverflow.com/questions/18827396/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe7-in-position-0-ordinal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!