calculate_Dday.py 1.62 KB
#-*- coding: utf-8 -*-
from datetime import datetime, timedelta
import json
import re

'''
def dday_calculate(date):
    y_m_d = date.split("-")
    return (datetime.now() - datetime(int(y_m_d[0]), int(y_m_d[1]), int(y_m_d[2]))).days


def date_calculate(url):
    dates = re.findall(r'[0-9]{2,4}[.|-][0-9]{1,2}[.|-][0-9]{1,2}|~', url)
    
    for i in range(len(dates)):
        dates[i] = dates[i].replace(".", "-")

    # 날짜가 없는 경우
    if len(dates) == 0:
        return "상시"

    # XXX ~
    if dates[-1] == "~":
        dday = dday_calculate(dates[0])
        if dday > 0:
            return "진행중"
        else:
            return "준비중"

    # ~ XXX and XXX ~ XXX
    if dates[-1] != "~":
        dday = dday_calculate(dates[-1])
        if dday < 0:
            return "D"+str(dday)
        else:
            return "종료"

    # except
    return "상 시"
'''


with open('d.json', 'rt', encoding='utf-8') as json_file:
    json_data = json.load(json_file)


for js in json_data:
    for key, value in js.items():
        if type(value) != str and key != 'View':
            js[key] = str(value)
        if key == "Link" and value[0] == '[':
            js[key] = value[1:-1]
#        if key == "Date":
#            js[key] = date_calculate(value)
            

with open('result.json', 'w', encoding='utf-8') as make_file:
    make_file.write('[')
    for i, js in enumerate(json_data):
        #make_file.write(json.dumps(js, indent="\t",ensure_ascii=False))
        json.dump(js, make_file, indent="\t", ensure_ascii=False)
        if i != len(json_data) - 1:
            make_file.write(',')
    make_file.write(']')