欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-07 19:58:20
...
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-8python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
  • 专题推荐

    作者信息
    python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法

    认证0级讲师

    推荐视频教程
  • python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法javascript初级视频教程
  • python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法jquery 基础视频教程
  • 视频教程分类