I'm pretty new to Python and am having trouble figuring out how to separate a year's worth of data into monthly CSV files.
A sample of the data I'm getting from the API call is as follows:
[{'counties': None,
'countryCode': 'CA',
'date': '2017-01-01',
'fixed': True,
'global': True,
'launchYear': None,
'localName': "New Year's Day",
'name': "New Year's Day",
'type': 'Public'},
{'counties': ['CA-BC'],
'countryCode': 'CA',
'date': '2017-02-13',
'fixed': False,
'global': False,
'launchYear': 2013,
'localName': 'Family Day',
'name': 'Family Day',
'type': 'Public'},
{'counties': ['CA-MB'],
'countryCode': 'CA',
'date': '2017-02-20',
'fixed': False,
'global': False,
'launchYear': None,
'localName': 'Louis Riel Day',
'name': 'Louis Riel Day',
'type': 'Public'},
etc...
I want to write to CSV by the 'date' of each holiday. Here is my code so far:
import json
import csv
import requests
import pprint
from datetime import datetime, date
class HolidaysByCountry:
def __init__(self, country_code, year):
self.country_code = country_code
self.year = year
def call_api(self):
url="http://date.nager.at/api/v1/get/{CountryCode}/{Year}".format(CountryCode=self.country_code, Year=self.year)
json_data = requests.get(url=url)
data = json.loads(json_data.text)
return data
def monthly_data(self):
api = self.call_api()
dates = []
for index in range(len(api)):
date_string = api[index]['date']
split_date = date_string.split("-")
month = split_date[1]
dates.append(month)
return dates
def save_to_csv(self):
api = self.call_api()
for month in self.monthly_data():
with open('./tmp/holidays_by_country_{month}.csv'.format(month=month, mode='w')) as file:
count = 0
writer = csv.writer(file)
for data in api:
if count == 0:
headers = data.keys()
writer.writerow(headers)
count += 1
writer.writerow(data.values())
file.close()
canada = HolidaysByCountry("CA","2017").save_to_csv()
Expected Output:
holidays_by_country_01.csv
date,localName,name,countryCode,fixed,global,counties,launchYear,type
2017-01-01,New Year's Day,New Year's Day,CA,True,True,,,Public
holidays_by_country_02.csv
date,localName,name,countryCode,fixed,global,counties,launchYear,type
2017-02-13,Family Day,Family Day,CA,False,False,['CA-BC'],2013,Public
2017-02-20,Louis Riel Day,Louis Riel Day,CA,False,False,['CA-MB'],,Public
etc...
Let me know if you need more information!