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

lluisu picture lluisu · Sep 16, 2013 · Viewed 33.8k times · Source

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

Answer

Simeon Visser picture Simeon Visser · Sep 16, 2013

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