0

I have a dict that I am trying to obtain certain data from, an example of this dict is as follows:

{
 'totalGames': 1,
 'dates': [{
 'totalGames': 1,
 'totalMatches': 0,
 'matches': [],
 'totalEvents': 0,
 'totalItems': 1,
 'games': [{
 'status': {
 'codedGameState': '7',
 'abstractGameState': 'Final',
 'startTimeTBD': False,
 'detailedState': 'Final',
 'statusCode': '7',
 },
 'season': '20172018',
 'gameDate': '2018-05-20T19:00:00Z',
 'venue': {'link': '/api/v1/venues/null',
 'name': 'Bell MTS Place'},
 'gameType': 'P',
 'teams': {'home': {'leagueRecord': {'wins': 9,
 'losses': 8, 'type': 'league'}, 'score': 1,
 'team': {'link': '/api/v1/teams/52',
 'id': 52, 'name': 'Winnipeg Jets'}},
 'away': {'leagueRecord': {'wins': 12,
 'losses': 3, 'type': 'league'}, 'score': 2,
 'team': {'link': '/api/v1/teams/54',
 'id': 54, 'name': 'Vegas Golden Knights'}}},
 'content': {'link': '/api/v1/game/2017030325/content'},
 'link': '/api/v1/game/2017030325/feed/live',
 'gamePk': 2017030325,
 }],
 'date': '2018-05-20',
 'events': [],
 }],
 'totalMatches': 0,
 'copyright': 'NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. \xa9 NHL 2018. All Rights Reserved.',
 'totalEvents': 0,
 'totalItems': 1,
 'wait': 10,
 }

I am interested obtaining the score for a certain team if they played that night, for example if my team of interest is the Vegas Golden Knights I would like to create a variable that contains their score (2 in this case). I am completely stuck on this so any help would be greatly appreciated!

alkasm
23.1k6 gold badges83 silver badges98 bronze badges
asked May 21, 2018 at 1:35

1 Answer 1

1

This just turns into ugly parsing but is easily doable following the JSON structure; would recommend flattening the structure for your purposes. With that said, if you'd like to find the score of a particular team on a particular date, you could do this:

def find_score_by_team(gamedict, team_of_interest, date_of_interest):
 for date in gamedict['dates']:
 for game in date['games']:
 if game['gameDate'].startswith(date_of_interest):
 for advantage in game['teams']:
 if game['teams'][advantage]['team']['name'] == team_of_interest:
 return game['teams'][advantage]['score']
 return -1

Example query:

>>> d = {'totalGames':1,'dates':[{'totalGames':1,'totalMatches':0,'matches':[],'totalEvents':0,'totalItems':1,'games':[{'status':{'codedGameState':'7','abstractGameState':'Final','startTimeTBD':False,'detailedState':'Final','statusCode':'7',},'season':'20172018','gameDate':'2018-05-20T19:00:00Z','venue':{'link':'/api/v1/venues/null','name':'BellMTSPlace'},'gameType':'P','teams':{'home':{'leagueRecord':{'wins':9,'losses':8,'type':'league'},'score':1,'team':{'link':'/api/v1/teams/52','id':52,'name':'WinnipegJets'}},'away':{'leagueRecord':{'wins':12,'losses':3,'type':'league'},'score':2,'team':{'link':'/api/v1/teams/54','id':54,'name':'VegasGoldenKnights'}}},'content':{'link':'/api/v1/game/2017030325/content'},'link':'/api/v1/game/2017030325/feed/live','gamePk':2017030325,}],'date':u'2018-05-20','events':[],}],'totalMatches':0,'copyright':'NHLandtheNHLShieldareregisteredtrademarksoftheNationalHockeyLeague.NHLandNHLteammarksarethepropertyoftheNHLanditsteams.\xa9NHL2018.AllRightsReserved.','totalEvents':0,'totalItems':1,'wait':10,}
>>> find_score_by_team(d, 'VegasGoldenKnights', '2018-05-20')
2

This returns -1 if the team didn't play that night, otherwise it returns the team's score.

answered May 21, 2018 at 2:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.