手机
当前位置:查字典教程网 >脚本专栏 >python >python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
摘要:Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8查看目前系统字符集复制...

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: 'module' object has no attribute 'setdefaultencoding'问题的解决方法】相关文章:

Python BeautifulSoup中文乱码问题的2种解决方法

python将html转成PDF的实现代码(包含中文)

python str与repr的区别

从零学python系列之新版本导入httplib模块报ImportError解决方案

python网络爬虫采集联想词示例

python分割和拼接字符串

Python编程语言的35个与众不同之处(语言特征和使用技巧)

python sqlobject(mysql)中文乱码解决方法

在python的WEB框架Flask中使用多个配置文件的解决方法

用python登录Dr.com思路以及代码分享

精品推荐
分类导航