AttributeError(“" str"对象没有属性"read"") [英] AttributeError("'str' object has no attribute 'read'")
本文介绍了AttributeError(“" str"对象没有属性"read"")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中出现错误:
In Python I'm getting an error:
Exception: (<type 'exceptions.AttributeError'>,
AttributeError("'str' object has no attribute 'read'",), <traceback object at 0x1543ab8>)
给出python代码:
Given python code:
def getEntries (self, sub):
url = 'http://www.reddit.com/'
if (sub != ''):
url += 'r/' + sub
request = urllib2.Request (url +
'.json', None, {'User-Agent' : 'Reddit desktop client by /user/RobinJ1995/'})
response = urllib2.urlopen (request)
jsonofabitch = response.read ()
return json.load (jsonofabitch)['data']['children']
此错误是什么意思,我是怎么引起的?
What does this error mean and what did I do to cause it?
推荐答案
问题是,对于json.load
,您应该传递定义了read
函数的类似对象的文件.因此,您可以使用 json.load(response)
或
The problem is that for json.load
you should pass a file like object with a read
function defined. So either you use json.load(response)
or json.loads(response.read())
.
这篇关于AttributeError(“" str"对象没有属性"read"")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文