Php Python';str';对象没有属性';阅读';
Python 3.3.2导入json&urllib.request Json 打印(response.info()) 代码 以下是我的代码 错误Php Python';str';对象没有属性';阅读';,php,python,arrays,json,python-3.x,Php,Python,Arrays,Json,Python 3.x,Python 3.3.2导入json&urllib.request Json 打印(response.info()) 代码 以下是我的代码 错误 回溯(最近一次呼叫最后一次): 文件“C:\Users\Jonathan\Desktop\python.py”,第57行,在 checkLink() 文件“C:\Users\Jonathan\Desktop\python.py”,第50行,在checkLink中 json_object=json.load(decodedRes) 文件“C:\Pytho
回溯(最近一次呼叫最后一次):
文件“C:\Users\Jonathan\Desktop\python.py”,第57行,在
checkLink()
文件“C:\Users\Jonathan\Desktop\python.py”,第50行,在checkLink中
json_object=json.load(decodedRes)
文件“C:\Python33\lib\json\\ uuuuu init\uuuuu.py”,第271行,正在加载中
返回加载(fp.read(),
AttributeError:“str”对象没有属性“read”
>>> .
知道如何解决这个问题吗?使用而不是json.load
json.loads(decodedRes)
- 接受类似文件的对象
尝试用
json.loads()
替换json.loads()
。前者需要一个文件流,这就是您遇到属性错误的原因。非常简单,您只需要以另一种方式导入beautiful soup。
现在您正在作为导入
from beautifulSoup import BeautifulSoup
换成
from bs4 import BeautifulSoup
下面的代码将json文件加载到文档中。文档将具有dict类型的值
file_name = "my_file.json"
with open(file_name, 'r') as f:
document = json.loads(f.read())
json.loads(decodedRes)
>>> import json
>>> json.load('{"a": 1}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\json\__init__.py", line 286, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
>>> json.loads('{"a": 1}')
{u'a': 1}
## decodedRes = response.read().decode('utf-8')
json_object = json.load(response)
from beautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
file_name = "my_file.json"
with open(file_name, 'r') as f:
document = json.loads(f.read())