I am having an issue finding documentation on writing to the same file through multiple days(Sorry for not using the proper wordage). Right now it opens and writes to 1 csv file per day(which at this time is about 60 seperate files for 1 day). Instead I would like all 60 days of iteration to save to 1 file. I thought the "a" stood for append which means to write to the same file but that is not accurate I am finding. I also commented out the outfile.close at the end thinking that was why. The end goal is to have everyday saved in 1 file with only 1 header.
SCRIPT:
import csv
import requests
import datetime
from pprint import pprint
import pendulum
start = pendulum.datetime(2018, 3, 29)
end = pendulum.today()
period = pendulum.period(start, end)
for dt in period.range('days'):
the_date = dt.format('YYYYMMDD')
outfile = open('Test_between_dates' + str(the_date) + '.csv',"a",newline='')
writer = csv.writer(outfile)
writer.writerow(["time","status",])
req2 = requests.get('https://api-prod.sprtactn.co/web/v1/scoreboard/mlb?bookIds=21,1,55&date=' + str(the_date) + '') #' + str(the_date) + '
odd = req2.json()['games']
for info in odd[0:]:
time = info['start_time']
status = info['status']
print(time, status)
writer.writerow([time, status])
## outfile.close()