【问题标题】:Unable to import json file into spyder无法将json文件导入spyder
【发布时间】:2025-12-29 01:00:11
【问题描述】:

这是我第一次使用任何类型的代码。我一直在关注一个交互式教程,但我似乎被困在第一步,试图导入一个包含有关足球比赛数据的信息的 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.

【问题讨论】:

  • 错误是自我解释的FileNotFoundError: [Errno 2] No such file or directory: 'Statsbomb/data/competitions.json'所以我会仔细检查你在with open('Statsbomb/data/competitions.json') as f:中的路径
  • 您好,感谢您的回复,如果我可以在文件资源管理器中查看所有文件。我应该包含绝对文件路径来尝试解决这个问题吗?
  • 不会是最强大的,但应该可以为您解决问题。
  • 当我更改为绝对文件路径时出现错误:JSONDecodeError: expecting value
  • 它可能不是一个有效的 json 文件。帮我一个忙,给我发一封电子邮件 jason.schvach@gmail.com 并包含该 json 文件,我会看看。您使用的是 Windows 吗?

标签: json directory path load


【解决方案1】:

使用 open('Statsbomb/data/matches/'+str(competition_id)+'/30.json') 作为 f: 匹配 = json.load(f) 尝试: 使用 open('Statsbomb/data/matches/'+str(competition_id)+'/3.json') 作为 f: 匹配 = json.load(f)

【讨论】: