简体   繁体   中英

Pendulum with python for period of time

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM