2017-04-15 61 views
-1

我想通過使用python下載的JSON文件打印本季的所有首要聯盟裝置。這裏是我正在使用的Json文件的鏈接 - Json File如何通過使用Python循環瀏覽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 
+0

你有壓痕PB在while循環 – Astrom

+0

謝謝,糾正。 – AaronC

回答

0

下面是一個簡單而直觀的解決方案,你所面對的問題:

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))