Python 根据一年的数据为每个月编写CSV文件

Python 根据一年的数据为每个月编写CSV文件,python,json,api,csv,Python,Json,Api,Csv,我对Python还很陌生,我很难弄清楚如何将一年的数据分割成每月的CSV文件 我从API调用中获得的数据示例如下: [{'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'}, {'

我对Python还很陌生,我很难弄清楚如何将一年的数据分割成每月的CSV文件

我从API调用中获得的数据示例如下:

[{'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...
我想在每个假期的“日期”之前写信给CSV。以下是我目前的代码:

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()
预期产出:

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...

如果您需要更多信息,请告诉我

我想这对你有用

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)

显示预期输出这里有几个问题。用这个熊猫代替Hi@Alderven,我添加了预期的输出,希望这有帮助@斯托夫,谢谢你的提示!谢谢你,巴尔德曼!代码给了我预期的准确输出!“收藏”图书馆对我来说是新的,所以我现在将复习如何使用它!