发布于 2014-08-26 13:40:24 | 3007 次阅读 | 评论: 1 | 来源: 网友投递

这里有新鲜出炉的Python教程,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


本文主要讲解了python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法,遇到相同问题的同学参考下.

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


最新网友评论  共有(1)条评论 发布评论 返回顶部
rqk 发布于2016-04-28 21:26:49
感觉很厉害的样子
支持(0)  反对(0)  回复

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务