Decoding nested JSON with multiple #39;for#39; loops(使用多个“for循环解码嵌套的 JSON)
问题描述
我是 Python 新手(上周),并且已经达到我的极限.花了三天的时间,我大部分时间都在 stackoverflow 上,但我不知道如何进一步!
Json 有多个嵌套数组.它可以包含三个(如下面的示例 (json.txt) 所示)或 30 个.我需要遍历每个,然后深入到局",最后获得小门"的值.这是我感到困惑的最后一步.谁能给点建议?
你完全绝望
会
import os, json,requests打印开始"url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt'# 下载json字符串json_string = requests.get(url)打印下载的json"# 获取内容the_data = json_string.json()打印 'the_data has length ', len(the_data)对于范围内的索引(len(the_data)):打印现在正在处理索引",索引对于 the_data[index] 中的检票口:打印检票口等于",检票口# 好的 - 我可以看到局.现在,我怎么进去# 并获得小门"? 解决方案 首先,不要使用索引,而是直接遍历列表;这样你就可以给它们起有意义的名字.顶层是一个条目列表,每个条目都是一个带有 'innings' 键的字典,每个 innings 是一个字典列表,其中包括:wickets 键:
用于输入数据:对于进入['局']的局:印刷局['小门']打印:
<预><代码>>>>用于输入数据:... 在条目 ['局'] 中的局:...打印局['小门']...10900
这样也可以更轻松地在每个级别添加信息:
<预><代码>>>>用于输入数据:...打印条目['描述']...对于我,在 enumerate(entry['innings']) 中的局:... 打印 'Innings {}: {} wickets'.format(i + 1, inning['wickets'])...2013 年 5 月 14 日,Pallekele 的斯里兰卡其他地区 v 斯里兰卡 A第 1 局:10 个小门第 2 局:9 个小门第 63 场比赛:皇家挑战者班加罗尔 v 国王十一旁遮普,2013 年 5 月 14 日在班加罗尔第 1 局:0 个小门第 2 局:0 个小门第 64 场比赛:2013 年 5 月 14 日,钦奈超级国王队对阵德里夜魔侠队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'?
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用多个“for"循环解码嵌套的 JSON
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
