Pendulum with python for period of time

1.7k Views Asked by At

Trying to run through each day and save as a separate CSV file with pendulum. Right now I am only able to get the first day of the period. Not sure if I need outfile or not but I am assuming I do since I want each separate CSV file to write, close, and start a new one.

import csv
import requests
import datetime
import pendulum

start = pendulum.datetime(2018, 1, 1)
end = pendulum.today()
period = pendulum.period(start, end)
for dt in period.range('days'):
    dt.format('YYYY-MM-DD')
    break

the_date = dt.format('YYYY-MM-DD')

outfile = open('TEST_PENDULUM_' + str(the_date) + '.csv',"w",newline='')
writer = csv.writer(outfile)
writer.writerow(["Date"])

req = requests.get('https://www.fantasylabs.com/api/lines/4/' + str(the_date) + '/startinggoalies') 
data = req.json()['GoalieMatchups']

for teams in data:
    HomeTeam = teams['Properties']['EventDate']

    print(HomeTeam)


    writer.writerow([HomeTeam])

outfile.close()
1

There are 1 best solutions below

0
On BEST ANSWER

You didn't write iterating logic on your codes.

import csv
import requests
import datetime
import pendulum

start = pendulum.datetime(2018, 1, 1)
end = pendulum.today()
period = pendulum.period(start, end)

for dt in period.range('days'):
    the_date = dt.format('YYYY-MM-DD')

    outfile = open('TEST_PENDULUM_' + str(the_date) + '.csv',"w",newline='')
    writer = csv.writer(outfile)
    writer.writerow(["Date"])

    req = requests.get('https://www.fantasylabs.com/api/lines/4/' + str(the_date) + '/startinggoalies')
    data = req.json()['GoalieMatchups']

    for teams in data:
        HomeTeam = teams['Properties']['EventDate']

        print(HomeTeam)
        writer.writerow([HomeTeam])

    outfile.close()