Doubt: how to fix the UnicodeEncodeError in Python?

2

Since I changed the machine, I have the following problem with the Python interpreter:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17: ordinal not in range(128)

All strings you send and receive are transforming into unicode. How can I fix this?

    
asked by anonymous 09.06.2015 / 15:02

1 answer

1

Below is an adapted translation of a response made to StackOverflow (the original) for a similar problem to yours. You can see the original question here with your answers.

Translation:

You're probably trying to print text that contains foreign Unicode characters - compared to the basic ASCII 128 - which may be our language. Try encode the Unicode string as ASCII 256 first:

unicodeData.encode('ascii', 'ignore')

' ignore ' will tell you to skip these characters. From the Python documentation:

>>> u = unichr(40960) + u'abcd' + unichr(1972)
>>> u.encode('utf-8')
'\xea\x80\x80abcd\xde\xb4'
>>> u.encode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in position 0: ordinal not in range(128)
>>> u.encode('ascii', 'ignore')
'abcd'
>>> u.encode('ascii', 'replace')
'?abcd?'
>>> u.encode('ascii', 'xmlcharrefreplace')
'&#40960;abcd&#1972;'

It may be useful to read the article " Absolute Absolute Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets. Excuses!) "*, which I think is a good tutorial for what is happening. After reading, you will stop feeling that you are just finding what the commands do (at least, that is what happened to me).

NOTE:

There is a translation of the mentioned article, as indicated by @jsbueno. This article is authored by Joel Spolsky and translated by Paulo André de Andrade. See here .     

09.06.2015 / 15:47