0

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()

1 Answer 1

1

outfile = open('Test_between_dates' + str(the_date) + '.csv',"a",newline='')

outfile is determined by the_date and therefore different every day.

Sign up to request clarification or add additional context in comments.

2 Comments

That makes sense, how would I go about changing that?
Give outfile a constant name. For example, you could just remove the str(the_date) part, yielding 'Test_between_dates.csv'.

Your Answer

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.