I got this very very frustrating error when inserting a certain string into my database. It said something like:
Python cannot decode byte characters, expecting unicode”
After a lot of searching, I saw that I could overcome this error by encoding my string into Unicode. I try to do this by decoding the string first and then encoding it in UTF-8 format. Like:
string = string.encode("utf8")
And I get the following error:
'ascii' codec can't decode byte 0xe3 in position 6: ordinal not in range(128)
I have been dying with this error! How do I fix it?
EDIT: As you can see from the downvotes, this is NOT THE BEST WAY TO DO IT. An excellent, and a highly recommended answer is immediately after this, so if you are looking for a good solution, please use that. This is a hackish solution that will not be kind to you at a later point of time.
I feel your pain, I’ve had a lot of problems with the same error. The simplest way I solved it (and this might not be the best way, and it depends on your application) was to convert things to unicode, and ignore errors. Here’s an example from Unicode HOWTO – Python v2.7.3 documentation
While this might not be the most expedient method, this is a method that has worked for me.
EDIT:
A couple of people in the comments have mentioned that this is a bad idea, even though the asker accepted the answer. It is NOT a great idea, it will screw things up if you are dealing with european and accented characters. However, this is something you can use if it is NOT production level code, if it is a personal project you are working on, and you need a quick fix to get things rolling. You will eventually need to fix it with the right methods, which are mentioned in the answers below.