python 生成 1900-2100 的二十四节气文件
[学习笔记] 转载
#!/usr/bin/python3.7
# -*- coding:utf-8 -*-
'''
整体思路
1:根据公式算出节气日期 1900 年到 2100 200 年的时间
2:特殊的年份进行纠正
3:保存到文件里去
'''
import sys
import json
import gc
import os
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
class jieqi:
# 计算节气的c常量组
c_list_21 = [3.87, 18.73, 5.63, 20.646, 4.81, 20.1, 5.52, 21.04, 5.678, 21.37, 7.108, 22.83, 7.5, 23.13, 7.646, 23.042, 8.318, 23.438, 7.438, 22.36, 7.18, 21.94, 5.4055, 20.12]
c_list_20 = [4.6295, 19.4599, 6.3826, 21.4155, 5.59,20.888, 6.318, 21.86, 6.5, 22.2, 7.928, 23.65, 8.35, 23.95, 8.44, 23.822, 9.098, 24.218, 8.218, 23.08, 7.9, 22.6, 6.11, 20.84]
# 节气名称组
name_arr = ["立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至", "小寒", "大寒"]
def __init__(self):
self.c_list=[]
## 特殊年份特殊节气进行纠正
def rectify_year(self,year,jieqiid,day):
## 特殊年份
rectify_year = [2026,2084,1911,2008,1902,1928,1925,2016,1922,2002,1927,1942,2089,2089,1978,1954,1918,2021,1982,2082,2019,2021]
## 特殊节气
rectify_jieqi = [1,3,6,7,8,9,10,10,11,12,14,15,17,18,19,20,21,21,22,22,23]
## 偏移量
rectify_offset = [-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1,-1,1,-1,1]
pop2 = -1
if year in rectify_year:
if year == 2089:
pop1 = rectify_year.index(year) ## 找到位置
pop2 = pop1+1
else:
pop1 = rectify_year.index(year) ## 找到位置
if rectify_jieqi[pop1] == jieqiid:
day = day + int(rectify_offset[pop1])
if rectify_jieqi[pop2] == jieqiid:
day = day + int(rectify_offset[pop2])
return day
#计算节气日期,并创建文件
def creat_year_jieqi(self,year):
year_pre = year//100
if year_pre == 19:
c_arr = self.c_list_20
elif year_pre == 20:
c_arr = self.c_list_21
year_num = year%100
list_arr = []
for i in range(0, 24):
c = c_arr[i]
## 注意:凡闰年3月1日前闰年数要减一,即:l=[(y-1)/4],因为小寒、大寒、立春、雨水这两个节气都小于3月1日,所以 y = y-1
if i == 0 or i == 1 or i == 22 or i == 23:
if self.comrun(year):
days = (year_num * 0.2422 + c) // 1 - ((year_num-1)// 4)
else:
days = (year_num * 0.2422 + c) // 1 - (year_num // 4)
else:
days = (year_num * 0.2422 + c) // 1 - (year_num // 4)
## 特殊年份节气进行纠正
days = self.rectify_year(year,i,days)
days = int(days)
days = '%02d' % days
y = int(year_num // 1)
m = i // 2 + 2
if m == 13:
m = 1
m = '%02d' % m
y = '%02d' % y
strs = "{3}{0}-{1}-{2} 00:00:00".format(str(y), str(m), str(days),str(year_pre))
item = dict(name=self.name_arr[i], jieqiid=str(i + 1), time=strs)
# print (item)
list_arr.append(item)
list_str = json.dumps(list_arr,ensure_ascii=false) ## 中文不进行编码
file_name = "./json/{0}.json".format(str(year))
with open(file_name, "w",encoding="utf-8") as f: ## 打开时用 utf-8 编码
json.dump(list_str, f,ensure_ascii=false)
print("{0}已载入文件完成...".format(str(year)))
# 读取年份为 name 年的节气数据
def read_json_file(self,name):
json_file = open('./json/' + name + '.json', 'r',encoding="utf-8")
json_str = json_file.read()
dic = json.loads(json_str)
print(dic)
# 读取测试
# read_json_file('2029')
# 读取所有年份的节气数据
def check_all_file(self):
for index in range(1900, 2100):
c_file_name = './json/{0}.json'.format(str(index))
if os.path.isfile(c_file_name):
json_file = open(c_file_name, 'r',encoding="utf-8")
json_str = json_file.read()
dic = json.loads(json_str)
print (str(index) + dic)
## 算是否是闰年
def comrun(self,year):
i = 0
if (year % 4) != 0 :
i=0
elif ((year % 100) == 0) & ((year % 400) != 0):
i=0
else:
i=1
return i
jieqi = jieqi()
#jieqi.creat_year_jieqi(2004)
#jieqi.read_json_file('2006')
#jieqi.check_all_file()
#
#for i in range(1900,2100):
# jieqi.creat_year_jieqi(i)
版权声明:本文为转载文章,原文地址:
上一篇: CentOS 网络配置
下一篇: JXstar平台相关开发工具介绍