[英]Python - iterate through nested json and save values
我有一个嵌套的JSON(API)Webstie,我想解析该项目并将其保存到文件(使用Scrapy框架)。
我想访问给定元素的每个子元素,它们的格式如下
0 {…}
1 {…}
2 {…}
3 {…}
4 {…}
5 {…}
6 {…}
7 {…}
8 {…}
9 {…}
10 {…}
如果我将元素0展开,则会得到以下值,其中{...}会进一步扩展
id 6738
date "2018-06-14T09:38:51"
date_gmt "2018-06-14T09:38:51"
guid
rendered "https:example.com"
modified "2019-03-19T20:43:50"
modified_gmt "2019-03-19T20:43:50"
我如何连续访问每个元素,首先是0,然后是1,然后是2 ... ...总计达到350,并获取例如的值
guid
rendered "https//:example.com"
并将其保存到项目。
我有的:
results = json.loads(response.body_as_unicode())
item = DataItem()
for var in results:
item['guid'] = results["guid"]
yield item
这失败了
TypeError: list indices must be integers, not str
我知道我可以使用
item['guid'] = results[0]["guid"]
但这只给了我整个列表的[0]索引,我想遍历所有索引。 如何在列表中传递索引号?
将for循环中的results["guid"]
替换为var["guid"]
:
for var in results:
item['guid'] = var["guid"]
# do whatever you want with item['guid'] here
当您可以像results[0]["guid"]
一样访问guid
时,这意味着您拥有字典列表,每个字典都包含名为guid
键。 在for循环中,使用results
(即列表)而不是抛出TypeError
的var
(每次迭代中包含每个词典)的var
,因为列表索引必须是整数而不是字符串(例如"guid"
)。
更新:如果要保存每个var["guid"]
,可以将其保存在这样的字典中:
guid_holder = {"guid": []}
for var in results:
guid_golder["guid].append(var["guid"])
for guid in guid_holder["guid"]:
print(guid)
现在guid_holder
包含所有元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.