使用多个'for'循环解码嵌套的JSON(Decoding nested JSON with multiple 'for' loops)

我是Python的新手(上周),已达到我的极限。 花了三天时间,我的大部分时间都在stackoverflow,但我无法解决如何进一步!

Json有多个嵌套数组。 它可能包含三个(如下例所示(json.txt))或30.我需要循环遍历每个,然后深入到'局'并最终获得'wickets'的值。 这是我感到困惑的最后一步。 任何人都可以建议吗?

你绝望了

import os, json,requests print 'Starting' url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt' # download the json string json_string = requests.get(url) print 'Downloaded json' # get the content the_data = json_string.json() print 'the_data has length ', len(the_data) for index in range(len(the_data)): print 'Now working on index ', index for wicket in the_data[index]: print 'wicket equals ',wicket # OK - I can see Innings. Now, how do I get inside # and obtain 'wickets'?

I'm new to Python (last week), and have reached my limit. Spent three days on this, most of my time in stackoverflow, but I cannot work out how to go any further!

The Json has multiple nested arrays. It could contain three (as the example below (json.txt) does), or 30. I need to loop through each, then drill down to 'innings' and finally get the value of 'wickets'. It's this last step that I'm confused by. Can anyone advise?

Yours in total desperation

Will

import os, json,requests print 'Starting' url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt' # download the json string json_string = requests.get(url) print 'Downloaded json' # get the content the_data = json_string.json() print 'the_data has length ', len(the_data) for index in range(len(the_data)): print 'Now working on index ', index for wicket in the_data[index]: print 'wicket equals ',wicket # OK - I can see Innings. Now, how do I get inside # and obtain 'wickets'?

最满意答案

首先,不要使用索引,而是直接在列表上循环; 这样你就可以给他们有意义的名字。 顶级是一个条目列表,每个条目都是一个带有'innings'键的字典,每个innings都是一个字典列表,其中包括一个wickets键:

for entry in data: for inning in entry['innings']: print inning['wickets']

这打印:

>>> for entry in data: ... for inning in entry['innings']: ... print inning['wickets'] ... 10 9 0 0

这样也可以更轻松地在每个级别添加信息:

>>> for entry in data: ... print entry['description'] ... for i, inning in enumerate(entry['innings']): ... print 'Innings {}: {} wickets'.format(i + 1, inning['wickets']) ... Rest of Sri Lanka v Sri Lanka A at Pallekele, May 14, 2013 Innings 1: 10 wickets Innings 2: 9 wickets 63rd match: Royal Challengers Bangalore v Kings XI Punjab at Bangalore, May 14, 2013 Innings 1: 0 wickets Innings 2: 0 wickets 64th match: Chennai Super Kings v Delhi Daredevils at Chennai, May 14, 2013

First of all, don't use an index but loop directly over the lists; that way you can give them meaningful names. The top-level is a list of entries, each entry is a dictionary with a 'innings' key, and each innings is a list of dictionaries, with, among others, a wickets key:

for entry in data: for inning in entry['innings']: print inning['wickets']

This prints:

>>> for entry in data: ... for inning in entry['innings']: ... print inning['wickets'] ... 10 9 0 0

This makes it easier to add information at each level too:

>>> for entry in data: ... print entry['description'] ... for i, inning in enumerate(entry['innings']): ... print 'Innings {}: {} wickets'.format(i + 1, inning['wickets']) ... Rest of Sri Lanka v Sri Lanka A at Pallekele, May 14, 2013 Innings 1: 10 wickets Innings 2: 9 wickets 63rd match: Royal Challengers Bangalore v Kings XI Punjab at Bangalore, May 14, 2013 Innings 1: 0 wickets Innings 2: 0 wickets 64th match: Chennai Super Kings v Delhi Daredevils at Chennai, May 14, 2013使用多个'for'循环解码嵌套的JSON(Decoding nested JSON with multiple 'for' loops)

我是Python的新手(上周),已达到我的极限。 花了三天时间,我的大部分时间都在stackoverflow,但我无法解决如何进一步!

Json有多个嵌套数组。 它可能包含三个(如下例所示(json.txt))或30.我需要循环遍历每个,然后深入到'局'并最终获得'wickets'的值。 这是我感到困惑的最后一步。 任何人都可以建议吗?

你绝望了

import os, json,requests print 'Starting' url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt' # download the json string json_string = requests.get(url) print 'Downloaded json' # get the content the_data = json_string.json() print 'the_data has length ', len(the_data) for index in range(len(the_data)): print 'Now working on index ', index for wicket in the_data[index]: print 'wicket equals ',wicket # OK - I can see Innings. Now, how do I get inside # and obtain 'wickets'?

I'm new to Python (last week), and have reached my limit. Spent three days on this, most of my time in stackoverflow, but I cannot work out how to go any further!

The Json has multiple nested arrays. It could contain three (as the example below (json.txt) does), or 30. I need to loop through each, then drill down to 'innings' and finally get the value of 'wickets'. It's this last step that I'm confused by. Can anyone advise?

Yours in total desperation

Will

import os, json,requests print 'Starting' url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt' # download the json string json_string = requests.get(url) print 'Downloaded json' # get the content the_data = json_string.json() print 'the_data has length ', len(the_data) for index in range(len(the_data)): print 'Now working on index ', index for wicket in the_data[index]: print 'wicket equals ',wicket # OK - I can see Innings. Now, how do I get inside # and obtain 'wickets'?

最满意答案

首先,不要使用索引,而是直接在列表上循环; 这样你就可以给他们有意义的名字。 顶级是一个条目列表,每个条目都是一个带有'innings'键的字典,每个innings都是一个字典列表,其中包括一个wickets键:

for entry in data: for inning in entry['innings']: print inning['wickets']

这打印:

>>> for entry in data: ... for inning in entry['innings']: ... print inning['wickets'] ... 10 9 0 0

这样也可以更轻松地在每个级别添加信息:

>>> for entry in data: ... print entry['description'] ... for i, inning in enumerate(entry['innings']): ... print 'Innings {}: {} wickets'.format(i + 1, inning['wickets']) ... Rest of Sri Lanka v Sri Lanka A at Pallekele, May 14, 2013 Innings 1: 10 wickets Innings 2: 9 wickets 63rd match: Royal Challengers Bangalore v Kings XI Punjab at Bangalore, May 14, 2013 Innings 1: 0 wickets Innings 2: 0 wickets 64th match: Chennai Super Kings v Delhi Daredevils at Chennai, May 14, 2013

First of all, don't use an index but loop directly over the lists; that way you can give them meaningful names. The top-level is a list of entries, each entry is a dictionary with a 'innings' key, and each innings is a list of dictionaries, with, among others, a wickets key:

for entry in data: for inning in entry['innings']: print inning['wickets']

This prints:

>>> for entry in data: ... for inning in entry['innings']: ... print inning['wickets'] ... 10 9 0 0

This makes it easier to add information at each level too:

>>> for entry in data: ... print entry['description'] ... for i, inning in enumerate(entry['innings']): ... print 'Innings {}: {} wickets'.format(i + 1, inning['wickets']) ... Rest of Sri Lanka v Sri Lanka A at Pallekele, May 14, 2013 Innings 1: 10 wickets Innings 2: 9 wickets 63rd match: Royal Challengers Bangalore v Kings XI Punjab at Bangalore, May 14, 2013 Innings 1: 0 wickets Innings 2: 0 wickets 64th match: Chennai Super Kings v Delhi Daredevils at Chennai, May 14, 2013