这是我第一次使用任何形式的代码。我一直在关注交互式教程,但似乎停留在第一步,试图导入一个包含有关足球比赛数据信息的json文件。似乎很简单,但是错误消息开始使我发疯后出现错误消息。
我正在尝试将数据加载到python中,以便跟着教程进行操作(我将在下面留下一个链接)。我相信我已经按照与教程中相同的方式保存了文件和数据,但是当我更改文件目录并运行时:import json如果有人可以建议我做错了什么,我会收到一些不同的错误消息,那就是不胜感激。我的目标是加载从GitHub下载的数据并打开竞赛JSON文件。
我也很高兴提供帮助回答此问题所需的任何信息。
YouTube视频:https://youtu.be/GTtu0t03FMO
错误消息:
FileNotFoundError: [Errno 2] No such file or directory: 'Statsbomb/data/competitions.json'
JSONDecodeError:Expecting value
#Load in Statsbomb competition and match data
#This is a library for loading json files.
import json
#Load the competition file
#Got this by searching 'how do I open json in Python'
with open('Statsbomb/data/competitions.json') as f:
competitions = json.load(f)
#Womens World Cup 2019 has competition ID 72
competition_id=72
#Womens World Cup 2019 has competition ID 72
competition_id=72
#Load the list of matches for this competition
with open('Statsbomb/data/matches/'+str(competition_id)+'/30.json') as f:
matches = json.load(f)
#Look inside matches
matches[0]
matches[0]['home_team']
matches[0]['home_team']['home_team_name']
matches[0]['away_team']['away_team_name']
#Print all match results
for match in matches:
home_team_name=match['home_team']['home_team_name']
away_team_name=match['away_team']['away_team_name']
home_score=match['home_score']
away_score=match['away_score']
describe_text = 'The match between ' + home_team_name + ' and ' + away_team_name
result_text = ' finished ' + str(home_score) + ' : ' + str(away_score)
print(describe_text + result_text)
#Now lets find a match we are interested in
home_team_required ="England"
away_team_required ="Sweden"
#Find ID for the match
for match in matches:
home_team_name=match['home_team']['home_team_name']
away_team_name=match['away_team']['away_team_name']
if (home_team_name==home_team_required) and (away_team_name==away_team_required):
match_id_required = match['match_id']
print(home_team_required + ' vs ' + away_team_required + ' has id:' + str(match_id_required))
#Exercise:
#1, Edit the code above to print out the result list for the Mens World cup
#2, Edit the code above to find the ID for England vs. Sweden
#3, Write new code to write out a list of just Sweden's results in the tournament.
答案 0 :(得分:0)
with open('Statsbomb/data/matches/'+str(competition_id)+'/30.json') as f: 匹配 = json.load(f) 尝试: with open('Statsbomb/data/matches/'+str(competition_id)+'/3.json') as f: 匹配 = json.load(f)