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


Posted in Python onAugust 22, 2014

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 相关文章推荐
gearman的安装启动及python API使用实例
Jul 08 Python
零基础写python爬虫之爬虫的定义及URL构成
Nov 04 Python
详解在Python程序中解析并修改XML内容的方法
Nov 16 Python
pycharm远程调试openstack代码
Nov 21 Python
python实现简易内存监控
Jun 21 Python
python utc datetime转换为时间戳的方法
Jan 15 Python
python3.6使用tkinter实现弹跳小球游戏
May 09 Python
如何使用python进行pdf文件分割
Nov 11 Python
python如何把字符串类型list转换成list
Feb 18 Python
详解python tcp编程
Aug 24 Python
python 对一幅灰度图像进行直方图均衡化
Oct 27 Python
Selenium结合BeautifulSoup4编写简单的python爬虫
Nov 06 Python
Python升级提示Tkinter模块找不到的解决方法
Aug 22 #Python
Python实现多行注释的另类方法
Aug 22 #Python
Python利用pyHook实现监听用户鼠标与键盘事件
Aug 21 #Python
Python发送Email方法实例
Aug 21 #Python
Python生成验证码实例
Aug 21 #Python
python中去空格函数的用法
Aug 21 #Python
python实现的重启关机程序实例
Aug 21 #Python
You might like
浅析php中如何在有限的内存中读取大文件
2013/07/02 PHP
php检测用户是否用手机(Mobile)访问网站的类
2014/01/09 PHP
PHP代码优化的53个细节
2014/03/03 PHP
PHP与jquery实时显示网站在线人数实例详解
2016/12/02 PHP
php批量删除操作代码分享
2017/02/26 PHP
php 判断页面或图片是否经过gzip压缩的方法
2017/04/05 PHP
PHP单元测试配置与使用方法详解
2019/12/27 PHP
jquery中html、val与text三者属性取值的联系与区别介绍
2013/12/29 Javascript
文本框水印提示效果的简单实现代码
2014/02/22 Javascript
JavaScript中自定义事件用法分析
2014/12/23 Javascript
修复bash漏洞的shell脚本分享
2014/12/31 Javascript
Node.js Addons翻译(C/C++扩展)
2016/06/12 Javascript
JS闭包与延迟求值用法示例
2016/12/22 Javascript
jQuery Validation Engine验证控件调用外部函数验证的方法
2017/01/18 Javascript
Jquery的autocomplete插件用法及参数讲解
2019/03/12 jQuery
[47:43]完美世界DOTA2联赛PWL S3 Magama vs GXR 第二场 12.19
2020/12/24 DOTA
Python基于Pymssql模块实现连接SQL Server数据库的方法详解
2017/07/20 Python
python实现扫描日志关键字的示例
2018/04/28 Python
用Python将mysql数据导出成json的方法
2018/08/21 Python
python 通过麦克风录音 生成wav文件的方法
2019/01/09 Python
Python3.5面向对象编程图文与实例详解
2019/04/24 Python
flask/django 动态查询表结构相同表名不同数据的Model实现方法
2019/08/29 Python
python实现音乐播放和下载小程序功能
2020/04/26 Python
欧洲著名的珠宝和手表网上商城:uhrcenter
2017/04/10 全球购物
装修致歉信
2014/01/15 职场文书
大学生2014全国两会学习心得体会
2014/03/10 职场文书
师范生自我鉴定
2014/03/20 职场文书
管理岗位竞聘演讲稿
2014/08/18 职场文书
2015年助理工程师工作总结
2015/04/03 职场文书
大学生村官工作总结2015
2015/04/09 职场文书
房租涨价通知
2015/04/23 职场文书
2015年信息技术教研组工作总结
2015/07/22 职场文书
初二数学教学反思
2016/02/17 职场文书
为什么MySQL选择Repeatable Read作为默认隔离级别
2021/07/26 MySQL
4种方法python批量修改替换列表中元素
2022/04/07 Python
使用CSS实现黑白格背景效果
2022/06/01 HTML / CSS