欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python錯誤:AttributeError: 'module' object has no attribute 'setdefaultencoding'問題的解決方法

 更新時間:2014年08月22日 09:20:53   投稿:junjie  
這篇文章主要介紹了python錯誤:AttributeError: 'module' object has no attribute 'setdefaultencoding'問題的解決方法,需要的朋友可以參考下

Python的字符集處理實在蛋疼,目前使用UTF-8居多,然后默認使用的字符集是ascii,所以我們需要改成utf-8
查看目前系統(tǒng)字符集

復制代碼 代碼如下:

import sys
print sys.getdefaultencoding()

執(zhí)行:
復制代碼 代碼如下:

[root@lee ~]# python a.py
ascii

修改成utf-8
復制代碼 代碼如下:

import sys
 
sys.setdefaultencoding('utf-8')
 
print sys.getdefaultencoding()

執(zhí)行:
復制代碼 代碼如下:

[root@lee ~]# python a.py
Traceback (most recent call last):
  File "a.py", line 4, in <module>
    sys.setdefaultencoding('utf-8')
AttributeError: 'module' object has no attribute 'setdefaultencoding'
提示:AttributeError: 'module' object has no attribute 'setdefaultencoding'?

后來經(jīng)過查找相關資料,才發(fā)現(xiàn)早期版本可以直接sys.setdefaultencoding('utf-8'),新版本需要先reload一下
復制代碼 代碼如下:

import sys
 
reload(sys)
sys.setdefaultencoding('utf-8')
 
print sys.getdefaultencoding()

執(zhí)行
復制代碼 代碼如下:

[root@lee ~]# python a.py
utf-8

 

相關文章

最新評論