【发布时间】:2020-02-16 22:00:29
【问题描述】:
从 JSON 文件 (Abbreviated example JSON file) 读取后,我有一个名为“liveData.plays.allPlays”的列表,其中包含许多具有键值对的字典。字典中的键相似,但有些有额外的键。我想打印所有唯一键,最终目标是在 CSV 文件中写入标题行时将它们用作字段名。我还需要将相应的值作为行写入 CSV 文件的下方。
我可以使用以下代码和整数 0 的操作来打印任何单个 dict 中的所有键:
with open('nhlbigtest2.json', 'r') as read_file:
data = json.load(read_file)
for k in data["liveData.plays.allPlays"][0].keys():
print(k)
这会产生:
about.dateTime
about.eventId
about.eventIdx
about.goals.away
about.goals.home
about.ordinalNum
about.period
about.periodTime
about.periodTimeRemaining
about.periodType
result.description
result.event
result.eventCode
result.eventTypeId
我可以将整数 0 操作为一个范围还是这个任务需要不同的方法? (使用 Python 3.7)
【问题讨论】:
标签: python json python-3.x dictionary