Wednesday, September 14, 2011

Solve the UnicodeError: ASCII Decoding/Encoding Error in Python

Sometimes we will get the Unicode Error in python, which means you are try to encode or decode a string which is not encoded in ASCII. ASCII is python default encoding, and what we need to do is just overwriting it to utf-8.
Put following codes in sitecustomize.py and put this file in your PYTHONPATH, which means it can be imported. This file may already exists somewhere in your PYTHONPATH, if so, just append the following codes in it. The file sitecustomize.py is a special file which is automatically imported when python restarts.
# sitecustomize.py
# this file can be anywhere in your Python path
import sys
sys.setdefaultencoding('utf-8')

No comments:

Post a Comment