ERROR 程序出错,错误原因:'bytes' object has no attribute 'read'
2024-10-19 04:34:04
使用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
最新文章
- 【C#公共帮助类】 Log4net 帮助类
- js日期重写
- 不错的判断 UITextView 内容不超过20个字符串的方法
- Unity破解for mac
- 93、App Links (应用程序链接)实例
- HDU 1506 Largest Rectangle in a Histogram
- hadoop的核心思想
- ibatis框架的sqlmapclient接口
- 关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析
- kill -9杀掉nginx主进程、reload失败解决办法
- 计蒜客NOIP模拟赛(2)D1T3 深黑幻想
- 如何让minicom换行
- [leetcode](4.21)4. 有效子数组的数目
- springboot之mybatis注解形式
- java-log4j日志打印
- lfs(systemv版本)学习笔记-第4页
- myeclipse和maven的clean和build
- Cordova笔记(一)
- C# 读写opc ua服务器,浏览所有节点,读写节点,读历史数据,调用方法,订阅,批量订阅操作
- TCP-IP and Advanced Topics 课程总结与报告