金狮镖局 Design By www.egabc.com

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

 

标签:
python,AttributeError

金狮镖局 Design By www.egabc.com
金狮镖局 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com

评论“python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法”

暂无python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法的评论...