栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题

Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8
查看目前系统字符集
复制代码 代码如下:
import sys
print sys.getdefaultencoding()

执行:
复制代码 代码如下:
[root@lee ~]# python a.py
ascii

修改成utf-8
复制代码 代码如下:
import sys
 
sys.setdefaultencoding('utf-8')
 
print sys.getdefaultencoding()

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

后来经过查找相关资料,才发现早期版本可以直接sys.setdefaultencoding('utf-8'),新版本需要先reload一下
复制代码 代码如下:
import sys
 
reload(sys)
sys.setdefaultencoding('utf-8')
 
print sys.getdefaultencoding()

执行
复制代码 代码如下:
[root@lee ~]# python a.py
utf-8

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/36047.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号