Skip to content

Instantly share code, notes, and snippets.

@tangotiger
Last active June 20, 2017 23:49
Show Gist options
  • Save tangotiger/a476914f7a8202144b1e2648ecf80d6c to your computer and use it in GitHub Desktop.
Save tangotiger/a476914f7a8202144b1e2648ecf80d6c to your computer and use it in GitHub Desktop.
NHL Schedule: JSON to csv/html
import json
print("Parse start")
# http://live.nhl.com/GameData/SeasonSchedule-20152016.json
sourcefile = "C:/Users/TOM/DataNHL/original/SeasonSchedule.json"
targetfile = "C:/Users/TOM/DataNHL/final/parsed_SeasonSchedule.csv"
htmltargetfile = "C:/Users/TOM/DataNHL/final/parsed_SeasonSchedule.htm"
header_row='NHL_GAME_ID' \
'|SEASON_ID' \
'|SUBSEASON_ID' \
'|GAME_ID' \
'|YEAR_GAME_DT' \
'|MONTH_GAME_DT' \
'|DAY_GAME_DT' \
'|HOUR_GAME_TM' \
'|MINUTE_GAME_TM' \
'|SECOND_GAME_TM' \
'|AWAY_ID' \
'|HOME_ID'
html_header_row = '<table border="1" cellpadding="4" cellspacing="0"><tr bgcolor="#CCCCCC">' \
'<th>NHL_GAME_ID</th>' \
'<th>SEASON_ID</th>' \
'<th>SUBSEASON_ID</th>' \
'<th>GAME_ID</th>' \
'<th>YEAR_GAME_DT</th>' \
'<th>MONTH_GAME_DT</th>' \
'<th>DAY_GAME_DT</th>' \
'<th>HOUR_GAME_TM</th>' \
'<th>MINUTE_GAME_TM</th>' \
'<th>SECOND_GAME_TM</th>' \
'<th>AWAY_ID</th>' \
'<th>HOME_ID</th>' \
'</tr>'
with open(sourcefile,'r') as infile \
, open(targetfile, 'w') as outfile \
, open(htmltargetfile, 'w') as htmloutfile:
json_data = json.load(infile)
outfile.write(header_row + '\n')
htmloutfile.write(html_header_row + '\n')
i=0
for d in json_data:
nhl_id=str(d['id']) # json field
year_id=int(nhl_id[0:4])
season_id=str(year_id) + str(year_id+1)
subseason_id=nhl_id[4:6]
game_id=nhl_id[6:10]
game_datetime=d['est'] # json field
split_game_datetime=game_datetime.split(' ')
game_date=split_game_datetime[0]
game_year=game_date[0:4]
game_month=game_date[4:6]
game_day=game_date[6:8]
game_time=split_game_datetime[1].split(':')
game_hour=game_time[0]
game_minute=game_time[1]
game_second=game_time[2]
team_away=d['a'] # json field
team_home=d['h'] # json field
outfile.write('{nhlid}'
'|{seasonid}'
'|{subseasonid}'
'|{gameid}'
'|{gameyear}'
'|{gamemonth}'
'|{gameday}'
'|{gamehour}'
'|{gameminute}'
'|{gamesecond}'
'|{teamaway}'
'|{teamhome}'
'\n'.format(
nhlid=nhl_id
, seasonid=season_id
, subseasonid=subseason_id
, gameid=game_id
, gameyear=game_year
, gamemonth=game_month
, gameday=game_day
, gamehour=game_hour
, gameminute=game_minute
, gamesecond=game_second
, teamaway=team_away
, teamhome=team_home
))
htmloutfile.write('<tr>'
'<td>{nhlid}</td>'
'<td>{seasonid}</td>'
'<td>{subseasonid}</td>'
'<td>{gameid}</td>'
'<td>{gameyear}</td>'
'<td>{gamemonth}</td>'
'<td>{gameday}</td>'
'<td>{gamehour}</td>'
'<td>{gameminute}</td>'
'<td>{gamesecond}</td>'
'<td>{teamaway}</td>'
'<td>{teamhome}</td>'
'</tr>'
'\n'.format(
nhlid=nhl_id
, seasonid=season_id
, subseasonid=subseason_id
, gameid=game_id
, gameyear=game_year
, gamemonth=game_month
, gameday=game_day
, gamehour=game_hour
, gameminute=game_minute
, gamesecond=game_second
, teamaway=team_away
, teamhome=team_home
))
i = i+1
htmloutfile.write('</table>\n')
print(str(i) + " : records written")
print("Parse end")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment