Created
April 8, 2018 12:49
-
-
Save inokappa/cdc3420209c208e11e3c8ffc5b2f5e6f to your computer and use it in GitHub Desktop.
内閣府が提供する祝日・休日情報の csv を JSON フォーマットで出力する Python スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import csv | |
import json | |
import requests | |
import boto3 | |
# def convertJson(): | |
# results = {} | |
# with open('syukujitsu_kyujitsu.csv2', encoding='shift_jis') as f: | |
# reader = csv.reader(f) | |
# header = next(reader) | |
# for line in reader: | |
# if line[1] == '休日': | |
# line[1] = '振替休日' | |
# results[line[0]] = line[1] | |
# | |
# j = json.dumps(results, ensure_ascii=False) | |
# | |
# return j | |
def convertJson(): | |
results = {} | |
with requests.Session() as s: | |
res = s.get('http://www8.cao.go.jp/chosei/shukujitsu/syukujitsu_kyujitsu.csv') | |
decoded_content = res.content.decode('shift_jis') | |
reader = csv.reader(decoded_content.splitlines(), delimiter=',') | |
header = next(reader) | |
for line in reader: | |
if line[1] == '休日': | |
line[1] = '振替休日' | |
results[line[0]] = line[1] | |
j = json.dumps(results, ensure_ascii=False) | |
return j | |
def putObject(contents, key): | |
session = boto3.session.Session() | |
if os.getenv('ENVIRONMENT') == 'debug': | |
s3 = session.client(service_name='s3', endpoint_url='http://127.0.0.1:5000/') | |
else: | |
s3 = session.client(service_name='s3') | |
s3.put_object(ACL='private', Body=contents, Bucket=os.getenv('BUCKET_NAME'), Key=key) | |
def saveObject(contents): | |
o = json.loads(contents) | |
years = [] | |
for k in o.keys(): | |
years.append(k.split('-')[0]) | |
for year in list(set(years)): | |
data = {} | |
for k, v in o.items(): | |
if year in k: | |
data[k] = v | |
putObject(json.dumps(data), '%s/data.json' % year) | |
putObject(contents, 'data.json') | |
def main(): | |
contents = convertJson() | |
saveObject(contents) | |
if __name__ == '__main__': | |
main() |
Author
inokappa
commented
Apr 8, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment