python UnicodeEncodeError, converting unicode to ascii

Tags:

In python, we often encounter the unicode convert issue. For instance, when you try to print a unicode string, you will get the following exception:

The reason is that  the str() function tries to convert the unicode string using ascii, which doesn’t support the character u’\xe6′.

The solution is to convert the string into ‘utf-8’ encoding.

recommended conversion workflow: input (any cp) -> convert to unicode -> (process) -> output to utf-8

See the following two examples:

Best practice:

Always encode from unicode to bytes.
In this direction, you get to choose the encoding.

The other way is to decode from bytes to unicode.
In this direction, you have to know what the encoding is.

 

Reference:

python UnicodeEncodeError, converting unicode to ascii

http://stackoverflow.com/questions/9644099/python-ascii-codec-cant-decode-byte