I'm totally new to Python, but am pretty well-versed in other languages so I have at least some idea of what's going on. I did not write this code, but I am trying to make it work for my purposes.
I'm using an API to retrieve statistics about every play in the NFL from last season. I am trying to write this information to a CSV file, and so far, it's writing the headers to the file and not the actual data.
Can someone tell me what I'm doing wrong?
import csv
import nflgame
games = nflgame.games_gen(2013, kind='REG')
plays = nflgame.combine_plays(games)
headers = []
for statId, info in nflgame.statmap.idmap.iteritems():
headers += info['fields']
headers.sort()
headers = ['gsis_id', 'drive_id', 'play_id', 'desc'] + headers
writer = csv.DictWriter(open('2013_regular_plays.csv', 'w+'), fieldnames=headers)
writer.writerow({k: k for k in headers})
for p in plays:
row = {k: getattr(p, k, 0) for k in headers}
row['gsis_id'] = p.drive.game.eid
row['drive_id'] = p.drive_num
row['play_id'] = p.playid
row['desc'] = p.desc
writer.writerow(row)
writer: it knows how to interpret field names.