python设置编码
import sys
sys.getdefaultencoding() #看到默认编码是'ascii'
#通常需要的是使用utf8编码,需要这样做:
reload(sys)
sys.setdefaultencoding("utf8")
sys.getdefaultencoding()
#but why?
python -v #启动解释器,可以看到一个 C:\Python27\lib\site.py
再这个文件里面有如下内容:
...
# Remove sys.setdefaultencoding() so that users cannot change the
# encoding after initialization. The test for presence is needed when
# this module is run as a script, because this code is executed twice.
if hasattr(sys, "setdefaultencoding"):
del sys.setdefaultencoding
....