python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的,此时需要自己设置将python的默认编码,一般设置为utf8的编码格式。

有三种方案:

方案一:(命令行,本会话有效)

1)通过>>>sys.getdefaultencoding()查看当前编码(若报错,先执行>>>import sys >>>reload(sys));

2)通过>>>sys.setdefaultencoding('utf8')设置编码

方案二:(较繁琐,最有效)

1)在程序文件中以下三句

import sys 
reload(sys) 
sys.setdefaultencoding('utf8')

方案三:(修改Python本环境永久有效,推荐)

在Python的Lib\site-packages文件夹下新建一个sitecustomize.py文件,内容为:

#coding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

重启Python解释器,发现编码已被设置为utf8,与方案二同效;这是因为系统在Python启动的时候,自行调用该文件,设置系统的默认编码,而不需要每次都手动加上解决代码,属于一劳永逸的解决方法。


 sitecustomize.py is a special script; Python will try to import it on startup, so any code in it will be run automatically. As the comment mentions, it can go anywhere (as long as import can find it), but it usually goes in the site-packages directory within your Python lib directory.
 setdefaultencoding function sets, well, the default encoding. This is the encoding scheme that Python will try to use whenever it needs to auto-coerce a unicode string into a regular string.