Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python decoding Unicode is not supported

I am having a problem with my encoding in Python. I have tried different methods but I can't seem to find the best way to encode my output to UTF-8.

This is what I am trying to do:

result = unicode(google.searchGoogle(param), "utf-8").encode("utf-8") 

searchGoogle returns the first Google result for param.

This is the error I get:

exceptions.TypeError: decoding Unicode is not supported 

Does anyone know how I can make Python encode my output in UTF-8 to avoid this error?

like image 768
simonbs Avatar asked Oct 03 '11 12:10

simonbs


People also ask

Is unicode supported in Python?

Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters. Unicode (https://www.unicode.org/) is a specification that aims to list every character used by human languages and give each character its own unique code.

What is decode (' UTF-8 ') in Python?

Encoding refers to encoding a string using an encoding scheme such as UTF-8 . Decoding refers to converting an encoded string from one encoding to another encoding scheme.

What is unicode encoding in Python?

Since Python 3.0, strings are stored as Unicode, i.e. each character in the string is represented by a code point. So, each string is just a sequence of Unicode code points. For efficient storage of these strings, the sequence of code points is converted into a set of bytes. The process is known as encoding.


1 Answers

Looks like google.searchGoogle(param) already returns unicode:

>>> unicode(u'foo', 'utf-8')  Traceback (most recent call last):   File "<pyshell#1>", line 1, in <module>     unicode(u'foo', 'utf-8') TypeError: decoding Unicode is not supported 

So what you want is:

result = google.searchGoogle(param).encode("utf-8") 

As a side note, your code expects it to return a utf-8 encoded string so what was the point in decoding it (using unicode()) and encoding back (using .encode()) using the same encoding?

like image 183
yak Avatar answered Oct 13 '22 04:10

yak