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
您可能感興趣的文章:
- 解決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進程崩潰AttributeError異常問題解決
- Pytorch出現(xiàn)錯誤Attribute?Error:module?‘torch‘?has?no?attribute?'_six'解決
相關文章
Python常見數(shù)據(jù)結構之棧與隊列用法示例
這篇文章主要介紹了Python常見數(shù)據(jù)結構之棧與隊列用法,結合實例形式簡單介紹了數(shù)據(jù)結構中棧與隊列的概念、功能及簡單使用技巧,需要的朋友可以參考下2019-01-01Flask框架URL管理操作示例【基于@app.route】
這篇文章主要介紹了Flask框架URL管理操作,結合實例形式分析了@app.route進行URL控制的相關操作技巧,需要的朋友可以參考下2018-07-07Python cookbook(數(shù)據(jù)結構與算法)保存最后N個元素的方法
這篇文章主要介紹了Python數(shù)據(jù)結構與算法 保存最后N個元素的方法,涉及Python基于迭代器與生成器實現(xiàn)歷史記錄功能的相關操作技巧,需要的朋友可以參考下2018-02-02windows下Anaconda的安裝與配置正解(Anaconda入門教程)
最近很多朋友學習python,很多朋友也推薦使用anaconda這個工具,但安裝以后也不會使用,這里腳本之家小編就為大家整理一下比較詳細的教程,方便自己也方便需要的朋友,希望大家以后多多支持腳本之家2018-04-04Python 在 VSCode 中使用 IPython Kernel 的方法詳解
這篇文章主要介紹了Python 在 VSCode 中使用 IPython Kernel 的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Python基于React-Dropzone實現(xiàn)上傳組件的示例代碼
本文主要介紹了在React-Flask框架上開發(fā)上傳組件的技巧。文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08