为了学习python,训练数据处理的能力,突发奇想,自己做一个老黄历进行查询。
仅限于交流学习,不得商用哈,数据是下载别人的,所以友善使用,学到就是赚到啦,创作不易转载请注明链接和出处
黄历数据下载地址:https://download.csdn.net/download/hq606/12314707 (黄历1900-2100黄历all.npy)
本人博客地址 :https://blog.csdn.net/hq606
作者 :hq606
做这个,好处是网上有大量的参考代码,可以参考学东西,主要用到
1.excel表格存为字典(网上有很多代码我就不写了)
2.字典的导入,合并,查询
第一步 .excel表格存为字典(网上有很多代码我就不写了,黄历数据自己随机下载一个,做成字典,就能啦)
第二步 .合并数据,做成字典,保存
有个字典的保存合并的代码挺好用的,直接放出来
import numpy as np
a=np.load('1900-1999黄历去重.npy',allow_pickle=True) #加载字典
b=np.load('2020-2099huangli.npy',allow_pickle=True)#加载字典
c=np.load('黄历2000-2019.npy',allow_pickle=True)#加载字典
print(len(a),len(b),len(c))
all=a.tolist()+c.tolist()+b.tolist() #转为列表格式
print(len(all))
#print(all)
np.save('1900-2099黄历all.npy', all) #保存字典数据
d=np.load('1900-2099黄历all.npy',allow_pickle=True)
print(d)
第三步 .下来就要用这个"1900-2099黄历all.npy"字典文件,查询实现日历功能。
黄历网上很多,自己做一个字典保存就好了,用numpy 保存和载入
**下面直接上代码,实现功能。**目前比较简单,然后做一个漂亮的可视化界面就更好了。
先看效果吧,字典里面有30多种黄历信息,现在只做了一部分信息出来,其他的相同方法提取字典查询即可
运行默认显示当前日期
然后可以随意查询你想看的日期
加了可视化界面以后
查询窗口
查询结果
代码如下,任意日期查询实现窗口化
import numpy as np
import calendar
import datetime
import time
import tkinter
from tkinter import *
def nowtime():
global y,m,d
today=datetime.date.today()
y=today.year
m=today.month
d=today.day
# 输入指定年月
# 显示日历
#print('-----------------------------------------')
#print('当前是',str(y),'年',str(m),'月 ',str(d),'日 日历')
#print(calendar.month(int(y),int(m)))
#print('-----------------------------------------')
return calendar.month(int(y),int(m))
#公历 农历 宜0 宜1 忌0 忌1 节日0 节日1 今日八字1 今日八字2 今日八字3 今日八字4 今日五行1 今日五行2 今日五行3 今日五行4 干支 生肖 星座 儒略日 胎神占方 财福喜神 年冲煞 月冲煞 日冲煞 24节气 日出日落时间 彭祖百忌 十二神 黄道
#nowtime()
def cx(year2,month,chiceday):
d=np.load('1900-2099黄历all.npy',allow_pickle=True)
if month<10:
day=str(year2)+'0'+str(month)+'月'
elif month>=10:
day=str(year2)+str(month)+'月'
mothday=[]
if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
for i in d:
if day in str(i['公历']):
#n+=1
mothday.extend([i])
#n+=1
#print(i)
a0='>>>>>>>>>>>>>>>>黄历信息<<<<<<<<<<<<<<<<'
a00='-----------------------------------------'
a=mothday[:][chiceday-1]['公历']
b=mothday[:][chiceday-1]['农历']
c=str('干支>>>>>')+str(mothday[:][chiceday-1]['干支'])
d=str(mothday[:][chiceday-1]['宜0'])+'>>>>>'+str(mothday[:][chiceday-1]['宜1'])
e=str(mothday[:][chiceday-1]['忌0'])+'>>>>>'+str(mothday[:][chiceday-1]['忌1'])
if mothday[:][chiceday-1]['节日']=='':
f=str('传统节日')+'>>>>>'+str('无')
else:
f=str('传统节日')+'>>>>>'+str(mothday[:][chiceday-1]['节日'])
g=str('财福喜神>>>>>')+str(mothday[:][chiceday-1]['财福喜神'])
h=str('24节气>>>>>')+str(mothday[:][chiceday-1]['24节气'])
i=str('日出日落时间>>>>>')+str(mothday[:][chiceday-1]['日出日落时间'])
j=str('彭祖百忌:')+str(mothday[:][chiceday-1]['彭祖百忌'].replace('\n\n',''))
k=str('黄道>>>>>')+str(mothday[:][chiceday-1]['黄道'].replace('??',''))
l=(day+'31天')
m='-----------------------------------------'
elif (month == 4 or month == 6 or month == 9 or month == 11 ):
for i in d:
if day in str(i['公历']):
#n+=1
mothday.extend([i])
#n+=1
a0='>>>>>>>>>>>>>>>>黄历信息<<<<<<<<<<<<<<<<'
a00='-----------------------------------------'
a=mothday[:-1][chiceday-1]['公历']
b=mothday[:-1][chiceday-1]['农历']
c=str('干支>>>>>')+str(mothday[:-1][chiceday-1]['干支'])
d=str(mothday[:-1][chiceday-1]['宜0'])+'>>>>>'+str(mothday[:-1][chiceday-1]['宜1'])
e=str(mothday[:-1][chiceday-1]['忌0'])+'>>>>>'+str(mothday[:-1][chiceday-1]['忌1'])
if mothday[:-1][chiceday-1]['节日']=='':
f=str('传统节日')+'>>>>>'+str('无')
else:
f=str('传统节日')+'>>>>>'+str(mothday[:-1][chiceday-1]['节日'])
g=str('财福喜神>>>>>')+str(mothday[:-1][chiceday-1]['财福喜神'])
h=str('24节气>>>>>')+str(mothday[:-1][chiceday-1]['24节气'])
i=str('日出日落时间>>>>>')+str(mothday[:-1][chiceday-1]['日出日落时间'])
j=str('彭祖百忌:')+str(mothday[:-1][chiceday-1]['彭祖百忌'].replace('\n\n',''))
k=str('黄道>>>>>')+str(mothday[:-1][chiceday-1]['黄道'].replace('??',''))
l=(day+'30天')
m='-----------------------------------------'
elif month == 2 and ((year % 4==0 and year % 100!=0) or (year % 400==0)):
for i in d:
if day in str(i['公历']):
mothday.extend([i])
#n+=1
a0='>>>>>>>>>>>>>>>>黄历信息<<<<<<<<<<<<<<<<'
a00='-----------------------------------------'
a=mothday[:-2][chiceday-1]['公历']
b=mothday[:-2][chiceday-1]['农历']
c=str('干支>>>>>')+str(mothday[:-2][chiceday-1]['干支'])
d=str(mothday[:-2][chiceday-1]['宜0'])+'>>>>>'+str(mothday[:-2][chiceday-1]['宜1'])
e=str(mothday[:-2][chiceday-1]['忌0'])+'>>>>>'+str(mothday[:-2][chiceday-1]['忌1'])
if mothday[:-2][chiceday-1]['节日']=='':
f=str('传统节日')+'>>>>>'+str('无')
else:
f=str('传统节日')+'>>>>>'+str(mothday[:-2][chiceday-1]['节日'])
g=str('财福喜神>>>>>')+str(mothday[:-2][chiceday-1]['财福喜神'])
h=str('24节气>>>>>')+str(mothday[:-2][chiceday-1]['24节气'])
i=str('日出日落时间>>>>>')+str(mothday[:-2][chiceday-1]['日出日落时间'])
j=str('彭祖百忌:')+str(mothday[:-2][chiceday-1]['彭祖百忌'].replace('\n\n',''))
k=str('黄道>>>>>')+str(mothday[:-2][chiceday-1]['黄道'].replace('??',''))
l=(day+'29天')
m='-----------------------------------------'
else:
for i in d:
if day in str(i['公历']):
#n+=1
mothday.extend([i])
a0='>>>>>>>>>>>>>>>>黄历信息<<<<<<<<<<<<<<<<'
a00='-----------------------------------------'
a=mothday[:-3][chiceday-1]['公历']
b=mothday[:-3][chiceday-1]['农历']
c=str('干支>>>>>')+str(mothday[:-3][chiceday-1]['干支'])
d=str(mothday[:-3][chiceday-1]['宜0'])+'>>>>>'+str(mothday[:-3][chiceday-1]['宜1'])
e=str(mothday[:-3][chiceday-1]['忌0'])+'>>>>>'+str(mothday[:-3][chiceday-1]['忌1'])
if mothday[:-3][chiceday-1]['节日']=='':
f=str('传统节日')+'>>>>>'+str('无')
else:
f=str('传统节日')+'>>>>>'+str(mothday[:-3][chiceday-1]['节日'])
g=str('财福喜神>>>>>')+str(mothday[:-3][chiceday-1]['财福喜神'])
h=str('24节气>>>>>')+str(mothday[:-3][chiceday-1]['24节气'])
i=str('日出日落时间>>>>>')+str(mothday[:-3][chiceday-1]['日出日落时间'])
j=str('彭祖百忌:')+str(mothday[:-3][chiceday-1]['彭祖百忌'].replace('\n\n',''))
k=str('黄道>>>>>')+str(mothday[:-3][chiceday-1]['黄道'].replace('??',''))
l=(day+'28天')
m='-----------------------------------------'
#print('\n\n')
return a0+'\n'+a00+'\n'+a+'\n'+b+'\n'+ c +'\n'+d+'\n'+e+'\n'+f+ '\n'+g+'\n'+h+'\n'+i+'\n'+j+'\n'+k+'\n'+l+'\n'+m
if __name__ == '__main__':
def newwind():
def show():
global year, month,chiceday
try:
year=int(ent_id1.get())
month=int(ent_id2.get())
chiceday=int(ent_id3.get())
year2=str(year)+'年'
print("年:%s" % year)
print("月:%s" % month)
print("日:%s" % chiceday)
print(cx(year2,month,chiceday))
frame = tkinter.Toplevel(winNew)
frame.title('查询结果')
if 1989<year<2100 and 0<chiceday:
tkinter.Message(frame, text=cx(year2,month,chiceday), width=500,font=("楷体",15),justify=tkinter.CENTER,fg = "red").grid(row=5, column=2)
else:
tkinter.Message(frame, text='输入错误重新输入,并补全信息', width=500,font=("楷体",15),justify=tkinter.CENTER,fg = "red").grid(row=5, column=2)
pass
except Exception as e:
frame = tkinter.Toplevel(winNew)
frame.title('查询结果')
tkinter.Message(frame, text='输入错误重新输入,并补全信息', width=500,font=("楷体",15),justify=tkinter.CENTER,fg = "red").grid(row=5, column=2)
ent_id1.delete(0, "end")
ent_id2.delete(0, "end")
ent_id3.delete(0, "end")
winNew = Toplevel(root)
winNew.geometry('320x240')
winNew.title('输入年月日查询黄历')
lab_id=Label(winNew,text='请输入年(1900-2099):')
btn_fd=Button(winNew,text='查询',command=show)
ent_id1=Entry(winNew)
btn_fd.grid(row = 0,column = 2,sticky = W)
lab_id.grid(row = 0,column = 0,sticky = W)
ent_id1.grid(row = 0,column = 1,sticky = W)
lab_id=Label(winNew,text='请输入月:')
ent_id2=Entry(winNew)
lab_id.grid(row = 1,column = 0,sticky = W)
ent_id2.grid(row = 1,column = 1,sticky = W)
lab_id=Label(winNew,text='请输入日:')
ent_id3=Entry(winNew)
lab_id.grid(row = 2,column = 0,sticky = W)
ent_id3.grid(row = 2,column = 1,sticky = W)
btClose=Button(winNew,text='关闭',command=winNew.destroy)
btClose.place(relx=0.7,rely=0.5)
def popupmenu(event):
mainmenu.post(event.x_root,event.y_root)
nowtime()
year2=str(y)+'年'
month = m
chiceday=d
root = Tk()
root.geometry('650x600')
root.resizable(0,0)
root.title('日历')
mainmenu = Menu(root)
menuFile = Menu(mainmenu,tearoff=0) # 菜单分组 menuFile
mainmenu.add_cascade(label="查询",menu=menuFile)
menuFile.add_command(label="点击开始查询",command=newwind)
menuFile.add_command(label="退出",command=root.destroy)
#photo =tkinter.PhotoImage("000.png")
theLabel = tkinter.Label(root,text='',justify=tkinter.CENTER,compound = tkinter.CENTER,font=("黑体",15),fg = "red",relief=GROOVE)
theLabel.pack()
msg1 = Message(root,text=nowtime(),fg='blue',font=("黑体",30),relief=GROOVE)
msg1.place(relx=0,y=350,relheight=0.4,width=650)
#lb = tkinter.Label(root,text='',anchor='center',image=photo,compound =tkinter.CENTER,fg='red',font=("黑体",15))
#lb.pack()
theLabel.configure(text=('当前是'+str(y)+'年'+str(m)+'月 '+str(d)+'日'+'\n'+cx(year2,month,chiceday)))
root.config(menu=mainmenu)
root.bind('Button-3',popupmenu) # 根窗体绑定鼠标右击响应事件
root.mainloop()
def testhuangli(): #这个是测试函数 窗口化以后 就不要啦
ctrl=1
while ctrl:
try:
global year, month,chiceday
nowtime()
year2=str(y)+'年'
#print('当前是',str(y),'年',str(m),'月 ',str(d),'日 日历')
print('当前是 '+str(y)+'年'+str(m)+'月'+str(d),'日\n',nowtime(),cx(year2,m,d))
print('-------------在下面输入具体日期查询黄历---------------')
flag=input('是否查询任意日期y or N: ')
if flag=='y':
year = int(input('输入Year(1900-2099): '))
month = int(input('输入Month: '))
year2=str(year)+'年'
chiceday=int(input('输入day: '))
print('显示查询月份的日历黄历信息','您查询的是',str(year),'年',str(month),'月', str(chiceday), '日 的日历')
#print(nowtime())
print(cx(year2,month,chiceday))
print('5秒后返回当天日历信息\n\n')
time.sleep(5)
print('返回当天日历信息')
time.sleep(1)
else:
print('已经退出日历')
ctrl=0
#findday()
except Exception as e:
print(e,'查询日期超出正常值,或输入格式非整数数字,请重新输入')