简体   繁体   中英

Write CSV file for each month from a year's worth of data

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!

I think this can work for you.

import requests
import csv
from collections import defaultdict

STATE = 'ca'
YEAR = 2017


def split_data_to_csv(state, year):
    data = defaultdict(list)
    headers = None
    r = requests.get('http://date.nager.at/api/v1/get/{}/{}'.format(state, year))
    if r.status_code == 200:
        entries = r.json()
        for entry in entries:
            month = entry['date'].split('-')[1]
            data[month].append(entry.values())
            if not headers:
                headers = entry.keys()

    for month, entries in data.items():
        with open('out_{}.csv'.format(month), 'wb') as out:
            writer = csv.writer(out)
            writer.writerow(headers)
            writer.writerows(entries)


split_data_to_csv(STATE, YEAR)

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