1. 程式人生 > >python 生成 1900-2100 的二十四節氣文件

python 生成 1900-2100 的二十四節氣文件

imp time tails reat python 位置 找到 __init__ 聲明

[學習筆記] 轉載

#!/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)

版權聲明:本文為轉載文章,原文地址:https://blog.csdn.net/rainredhezhang/article/details/88824524

python 生成 1900-2100 的二十四節氣文件