python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法,setdefaultencoding
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法,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
是python 3版本吧,要用urllib.parse
import urllib.parseimport urllib.requesturl = 'www.baidu.com/s'user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'values = {'name' : 'WHY', 'location' : 'SDU', 'language' : 'Python', 'ie' : 'utf-8', 'wd' : 'python' }headers = { 'User-Agent' : user_agent }data = urllib.parse.urlencode(values)#data=data.encode(encoding='UTF8')req = urllib.request.Request(url+'?'+data)#, data, headers)response = urllib.request.urlopen(req)the_page = response.read()print(the_page.decode('UTF8'))
from icmodel import independent_cascade 已经导入了
nx.independent_cascade 前面的nx.删掉
H = independent_cascade(DG,[6])
相关内容
- Python中还原JavaScript的escape函数编码后字符串的方法,
- python with statement 进行文件操作指南,
- Python with的用法,Pythonwith用法
- Python安装Imaging报错:The _imaging C module is not installed问题
- Python中让MySQL查询结果返回字典类型的方法,pythonmysq
- Python使用MD5加密字符串示例,pythonmd5
- Python实现的石头剪子布代码分享,python石头剪子布
- Python解释执行原理分析,python解释原理
- Python实现远程调用MetaSploit的方法,pythonmetasploit
- Python实现Linux下守护进程的编写方法,python守护进程
评论关闭