Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 889
lluisu Avatar asked Sep 16 '13 11:09

lluisu


People also ask

How do I decode a UTF 8 string in Python?

Use bytes.decode(encoding) with encoding as "utf8" to decode a UTF-8-encoded byte string bytes .


2 Answers

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')
like image 162
Simeon Visser Avatar answered Oct 06 '22 03:10

Simeon Visser


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

>>> u'ç'.encode('utf8')
'\xc3\xa7'
like image 32
Ashwini Chaudhary Avatar answered Oct 06 '22 03:10

Ashwini Chaudhary