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居多,然后默認(rèn)使用的字符集是ascii,所以我們需要改成utf-8
查看目前系統(tǒng)字符集
復(fù)制代碼 代碼如下:
import sys
print sys.getdefaultencoding()
執(zhí)行:
復(fù)制代碼 代碼如下:
[root@lee ~]# python a.py
ascii
修改成utf-8
復(fù)制代碼 代碼如下:
import sys
sys.setdefaultencoding('utf-8')
print sys.getdefaultencoding()
執(zhí)行:
復(fù)制代碼 代碼如下:
[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)過查找相關(guān)資料,才發(fā)現(xiàn)早期版本可以直接sys.setdefaultencoding('utf-8'),新版本需要先reload一下
復(fù)制代碼 代碼如下:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print sys.getdefaultencoding()
執(zhí)行
復(fù)制代碼 代碼如下:
[root@lee ~]# python a.py
utf-8
您可能感興趣的文章:
- 解決python多線程報錯:AttributeError: Can''t pickle local object問題
- Python3下錯誤AttributeError: ‘dict’ object has no attribute’iteritems‘的分析與解決
- Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯誤問題解決方案
- 解決AttributeError:'NoneTypeobject'?has?no?attribute'Window'的問題(親測有效)
- Python進(jìn)程崩潰AttributeError異常問題解決
- Pytorch出現(xiàn)錯誤Attribute?Error:module?‘torch‘?has?no?attribute?'_six'解決
相關(guān)文章
python輾轉(zhuǎn)相除法求最大公約數(shù)和最小公倍數(shù)的實現(xiàn)
這篇文章主要介紹了python輾轉(zhuǎn)相除法求最大公約數(shù)和最小公倍數(shù)的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Flask和pyecharts實現(xiàn)動態(tài)數(shù)據(jù)可視化
這篇文章主要介紹了Flask和pyecharts實現(xiàn)動態(tài)數(shù)據(jù)可視化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
修改Python?pip下載包的默認(rèn)路徑詳細(xì)步驟記錄
這篇文章主要介紹了如何修改pip的默認(rèn)安裝路徑以釋放C盤空間,特別是針對機器學(xué)習(xí)相關(guān)的大型包,可以將pip的安裝位置更改為其他目錄,需要的朋友可以參考下2025-03-03

