转载来源:http://shirley-ren.iteye.com/blog/1018750
python在安装时,默认的编 码是ascii,当程序中出现非ascii编码时,
常常会报这样的错UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的
查询系统默认编码可以在解释器中输入以下命令:
>>> import sys
>>> sys.getdefaultencoding()
'utf8'
方案一(推荐.一劳永逸) 在python的lib\site-packages(/usr/lib/python2.6/site-packages/sitecustomize.py)文件夹下新建一个sitecustomize.py
点击(此处)折叠或打开
- #!/usr/bin/env python
- # encoding=utf8
- import sys
- reload(sys)
- sys.setdefaultencoding('utf8')
方案二:在程序中加入以下代码(每个程序都需要添加,比较麻烦)
点击(此处)折叠或打开
- import sys
- reload(sys)
- sys.setdefaultencoding('utf8')