ERROR 程序出错,错误原因:'bytes' object has no attribute 'read'
使用json解析数据时,通常遇到这里就会出现问题'bytes' object has no attribute 'read',这是由于使用的json内置函数不同,一个是load另一个是loads。
import urllib.request
import json response = urllib.request.urlopen('http://www.reddit.com/r/all/top/.json').read()
jsonResponse = json.load(response) for child in jsonResponse['data']['children']:
print (child['data']['title'])
通常解决方式有两种,一种是更改函数为loads,另一种是更改编码格式为utf8
第一种解决方式:
jsonResponse = json.loads(response.decode('utf-8'))
第二种解决方式
使用json.loads()而不是json.load()
内容参考:https://stackoverflow.com/questions/6541767/python-urllib-error-attributeerror-bytes-object-has-no-attribute-read
ERROR 程序出错,错误原因:'bytes' object has no attribute 'read'的更多相关文章
- pyqt(二) 创建第一个程序(helloworld)解决object has no attribute 'setCentralWidget'
1.运行Qt Creator QtCreator主界面分为了6个模式:欢迎模式.编辑模式.设计模式.Debug调试模式.项目模式和帮助模式,分别由左侧的6个图标进行切换,对应的快捷键是Ctrl + 数 ...
- python 错误AttributeError: 'module' object has no attribute 'AF_INET'
写了一个简单的python socket的程序.运行时,报错如下 原因:文件的命名与Python的function的命名冲突 修改名称后,发现还是无法运行,检查目录下面是否有 这样子的一个文件,删除即 ...
- python调用对象属性出错:AttributeError: 'function' object has no attribute '_name_'
出错如下图所示: 原来是因为把__name__写成_name_, 下图为正确结果:
- AttributeError: 'bytes' object has no attribute 'hex'
python3.5之前bytes数据没有hex()属性 需要使用 ''.join(map(lambda x:('' if len(hex(x))>=4 else '/x0')+hex(x)[2: ...
- python3错误AttributeError: 'TestSequenceFunctions' object has no attribute 'seq'
对比了两段代码发现,原来是setUp要用用大写才能被正确引用. 修改后,代码运行成功.
- 'Service' object has no attribute 'process'
在使用selenium+phantomjs时,运行总是出现错误信息: 'Service' object has no attribute 'process' 出现该错误的原因是未能找到可执行程序&qu ...
- AttributeError: 'WebElement' object has no attribute 'send_keys'
这个是没问题的代码:用来打开谷歌搜索cheese并退出 from selenium import webdriver from selenium.common.exceptions import Ti ...
- error C2220: 警告被视为错误 - 没有生成“object”文件
原文:error C2220: 警告被视为错误 - 没有生成"object"文件 这种错误的原因是:原因是该文件的代码页为英文,而我们系统中的代码页为中文. 解决方案: 1. ...
- [Android Studio导入第三方类库方法] Error:(19, 23) 错误: 程序包org.apache.http不存在
本文主要参考以下链接: http://m.blog.csdn.net/blog/BoxRice/48575027 https://drakeet.me/android-studio http://ww ...
随机推荐
- pycharm 教程(一)安装和首次使用
PyCharm 是我用过的Python编辑器中,比较顺手的一个.而且可以跨平台,在 mac 和 windows 下面都可以用,这点比较好. 首先预览一下 PyCharm 在实际应用中的界面:(更改了P ...
- iOS笔记之网络
用POST方式上传数据时,数组怎么处理? NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data ...
- MYeclipes项目导入导出
导入: 右键,import,
- L201
The American public’s obsession with dieting has led to one of the most dangerous healthmisconceptio ...
- Mac下 cordova 安装随笔
首先这是我自己第一篇博客,如果有什么不对的,大家指出,积极修改. cordova是大家做混合开发最经常使用的一款使用HTML, CSS & JS进行移动App开发多平台共用一套代码,中文官方网 ...
- 2017年7月ROS学习资料小结
<孙子兵法·谋攻篇>:"上兵伐谋,其次伐交,其次伐兵,其下攻城:攻城之法为不得已." 任何发生在自己国土上的战争,即便胜利,也饱含屈辱. ----~~~~----Gaz ...
- ZetCode PyQt4 tutorial widgets II
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- USB学习笔记-协议
一.USB设备枚举过程 1.复位从设备使其设备地址为02.先从设备发送读取设备描述符的命令(只读取一次,即使端点0的最大包长小于18字节)3.设备返回设备描述符4.主机返回0长度确认数据包给到设备5. ...
- git hooks 的学习使用
示例网址: https://my.oschina.net/u/3477605/blog/1806238 https://www.jianshu.com/p/935409ce4c9a https://w ...
- pgsql的同步须知
pgsql的同步模式是根据master上的日志来做的同步,有两种同步方式,参考http://www.chinaxing.org/articles/Postgres/2012/12/14/2012-12 ...