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

34,858

Solution 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')

Solution 2

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

>>> u'ç'.encode('utf8')
'\xc3\xa7'
Share:
34,858
lluisu
Author by

lluisu

Updated on May 15, 2020

Comments

  • lluisu
    lluisu almost 4 years

    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