python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8
查看目前系统字符集
importsys printsys.getdefaultencoding()
执行:
[root@lee~]#pythona.py ascii
修改成utf-8
importsys
sys.setdefaultencoding('utf-8')
printsys.getdefaultencoding()
执行:
[root@lee~]#pythona.py
Traceback(mostrecentcalllast):
File"a.py",line4,in<module>
sys.setdefaultencoding('utf-8')
AttributeError:'module'objecthasnoattribute'setdefaultencoding'
提示:AttributeError:'module'objecthasnoattribute'setdefaultencoding'?
后来经过查找相关资料,才发现早期版本可以直接sys.setdefaultencoding('utf-8'),新版本需要先reload一下
importsys
reload(sys)
sys.setdefaultencoding('utf-8')
printsys.getdefaultencoding()
执行
[root@lee~]#pythona.py utf-8
热门推荐
10 感谢韩语老师祝福语简短
11 升职文案高级祝福语简短
12 花童结婚祝福语大全简短
13 向国庆送祝福语简短
14 什么寄语简短祝福语女生
15 客户回礼祝福语大全简短
16 女孩生日祝福语简短创意
17 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短