从一年的数据中为每个月编写 CSV 文件
Posted
技术标签:
【中文标题】从一年的数据中为每个月编写 CSV 文件【英文标题】:Write CSV file for each month from a year's worth of data 【发布时间】:2019-07-18 00:41:40 【问题描述】:我对 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...
如果您需要更多信息,请告诉我!
【问题讨论】:
显示预期输出 这里有几个问题。使用这个pandas
而不是pandas-dataframe-groupby-datetime-month
嗨@Alderven,我添加了预期的输出,希望对您有所帮助!
@stovfl,感谢您的提示!
【参考方案1】:
我认为这对你有用。
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)
【讨论】:
谢谢秃头!代码按预期给了我确切的输出! 'collections' 库对我来说是新的,所以我现在将复习如何使用它!以上是关于从一年的数据中为每个月编写 CSV 文件的主要内容,如果未能解决你的问题,请参考以下文章