Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AttributeError(“str';对象没有属性';read';”)_Python_Python 2.7_Urllib2_Attributeerror - Fatal编程技术网

Python AttributeError(“str';对象没有属性';read';”)

Python AttributeError(“str';对象没有属性';read';”),python,python-2.7,urllib2,attributeerror,Python,Python 2.7,Urllib2,Attributeerror,在Python中,我得到一个错误: Exception: (<type 'exceptions.AttributeError'>, AttributeError("'str' object has no attribute 'read'",), <traceback object at 0x1543ab8>) 这个错误是什么意思?我做了什么导致了它?问题是,对于json.load,您应该传递一个定义了read函数的类文件对象。所以要么你用要么 这正

在Python中,我得到一个错误:

Exception:  (<type 'exceptions.AttributeError'>,
AttributeError("'str' object has no attribute 'read'",), <traceback object at 0x1543ab8>)

这个错误是什么意思?我做了什么导致了它?

问题是,对于
json.load
,您应该传递一个定义了
read
函数的类文件对象。所以要么你用要么

这正是它所说的:有东西试图在你给它的对象上找到一个
.read
属性,你给了它一个
str
类型的对象(即,你给了它一个字符串)

此处发生错误:

json.load (jsonofabitch)['data']['children']
好吧,您没有在任何地方寻找
read
,因此它必须发生在您调用的
json.load
函数中(如完整回溯所示)。这是因为
json.load
正在尝试
。读取您给它的东西,但您给了它
jsonofabitch
,它当前命名一个字符串(您在
响应上调用
.read

解决方法:不要打电话给
。自己阅读
;函数将执行此操作,并希望您直接给它
响应
,以便它可以执行此操作


您还可以通过阅读函数的内置Python文档(try
help(json.load)
,或整个模块的内置Python文档(try
help(json)
),或者通过查看上的这些函数的文档来解决此问题。

如果您遇到类似于这样的Python错误:

AttributeError: 'str' object has no attribute 'some_method'
#!/usr/bin/env python
import json
def foobar(json):
    msg = json.loads(json)

foobar('{"batman": "yes"}')
AttributeError: 'str' object has no attribute 'loads'
#!/usr/bin/env python
import json
def foobar(jsonstring):
    msg = json.loads(jsonstring)

foobar('{"batman": "yes"}')
您可能意外地用字符串覆盖对象,从而使对象中毒

如何用几行代码在python中重现此错误:

AttributeError: 'str' object has no attribute 'some_method'
#!/usr/bin/env python
import json
def foobar(json):
    msg = json.loads(json)

foobar('{"batman": "yes"}')
AttributeError: 'str' object has no attribute 'loads'
#!/usr/bin/env python
import json
def foobar(jsonstring):
    msg = json.loads(jsonstring)

foobar('{"batman": "yes"}')
运行它,它会打印:

AttributeError: 'str' object has no attribute 'some_method'
#!/usr/bin/env python
import json
def foobar(json):
    msg = json.loads(json)

foobar('{"batman": "yes"}')
AttributeError: 'str' object has no attribute 'loads'
#!/usr/bin/env python
import json
def foobar(jsonstring):
    msg = json.loads(jsonstring)

foobar('{"batman": "yes"}')
但是更改variablename的名称,它就可以正常工作了:

AttributeError: 'str' object has no attribute 'some_method'
#!/usr/bin/env python
import json
def foobar(json):
    msg = json.loads(json)

foobar('{"batman": "yes"}')
AttributeError: 'str' object has no attribute 'loads'
#!/usr/bin/env python
import json
def foobar(jsonstring):
    msg = json.loads(jsonstring)

foobar('{"batman": "yes"}')

此错误是在尝试在字符串中运行方法时引起的。字符串有几个方法,但不是您正在调用的方法。因此,请停止尝试调用字符串未定义的方法,并开始查找对象中毒的位置。

好的,这是一个旧线程,但是。 我也有同样的问题,我的问题是我使用了
json.load
而不是
json.load

这样,json加载任何类型的字典都没有问题

json.load-使用此转换表将fp(a.read()-支持包含json文档的文本文件或二进制文件)反序列化为Python对象

loads-使用此转换表将s(包含json文档的str、bytes或bytearray实例)反序列化为Python对象


您需要先打开文件。这不起作用:

json\u file=json.load('test.json')
但这是可行的:

f=open('test.json'))
json_file=json.load(f)

我不明白这一点……read()如何解决问题?响应仍然没有read函数。我们应该将字符串放在某个具有read函数的对象中吗?@yourfriendzak仔细阅读,这是两个名称非常相似的不同函数。
json.load()
使用一个类似文件的对象和一个
read()
method,
json.loads()
接受一个字符串。很容易漏掉结尾的“s”并认为它们是同一个方法。感谢Joshmaker的评论,json.loads()可以解析json数据的字符串!@yourfriendzak这会告诉你,使用
open
可以实现这一点。@MANISHZOPE
s
代表“字符串”.我同意标准图书馆在如何命名方面存在一些严重问题,这是一个很好的例子,说明了它是如何变得混乱的。Stackoverflow的主持人似乎有幽默感,换个角度来说……2019年,这仍然很有趣:Pindeded,《我的一天》,来自未来的感谢:)这就是OP所说的。在帮助他人时,我总是在犹豫是否要更改或保留这样的标识符名称/哦,你说得对,我已经略过了。。。但是我没有抱怨:)我在试图打开文件而不是问题中的请求-响应时,在问题中发现了错误。显然,在后端,json对两者的处理是相似的,所以这个答案对我很有帮助。很明显值得投一票。“负载”对我来说很有用。谢谢你的解决方案