我正在尝试通过我下载的 JSON 文件使用 python 打印本赛季的所有英超联赛赛程。这是我正在使用的 Json 文件的链接 - Json 文件
我设法使用 while 循环打印了第一个比赛日的固定装置。我想要日期,主队,“vs”,客队。我想我需要使用外循环来循环其他比赛日,但我需要这方面的帮助。
import json
with open('en.1.json') as json_data:
data = json.load(json_data)
matchday = data['rounds'][0]['matches']
i = 0
while i < len(matchday):
home = matchday[i]['team1']
away = matchday[i]['team2']
date = matchday[i]['date']
print date, home['name'], "vs", away['name']
i = i + 1
这是您面临的问题的简单直观的解决方案:
with open(premierleaguedatafile) as data_file:
# loads the entire dataset into a dictionary
data = json.load(data_file)
# get the list of all the rounds of fixtures
rounds = data['rounds']
# iterate over each round
for matchday in rounds:
# store the list of matches played on this matchday
matches = matchday['matches']
# iterate over the matches to get individual match details
for match in matches:
match_date = match['date']
home_team = match['team1']['name']
away_team = match['team2']['name']
print('{} {} vs {}'.format(match_date, home_team, away_team))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句