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
    ....

posted on 2017-12-29 13:34  blbl9527  阅读(147)  评论(0编辑  收藏  举报

导航


github sina微博 FB