[英]Python for loop iterating 'i'
I am using the following script: 我使用以下脚本:
tagRequest = requests.get("https://api.instagram.com/v1/tags/" + tag + "/media/recent?client_id=" + clientId)
tagData = json.loads(tagRequest.text)
tagId = tagData["data"][0]["user"]["id"]
for i in tagData["data"]:
print tagData["data"][i]
My script is supposed to iterate over the JSON object, tagData. 我的脚本应该迭代JSON对象tagData。 (Over everything in "data".) However, I am getting the following error: list indices must be integers, not dict.
(在“数据”中的所有内容。)但是,我收到以下错误: list indices must be integers, not dict.
You are iterating over the contents of tagData['data']
not its indices, so: 您正在遍历tagData['data']
的内容而不是其索引,因此:
for i in tagData["data"]:
print i
Or indices: 或指数:
for i in xrange(len(tagData["data"])):
print tagData["data"][i]
if you use enumerate(iterable) you can access the index and the foreach item: 如果使用enumerate(iterable),则可以访问索引和foreach项:
for i,item in enumerate(tagData["data"]):
print i
print item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.