Python统计节假日剩余天数的脚本
1、前言
如何快速的想了解距离节假日还有多少天?
接下来使用Python脚本来解决这个问题。
2、倒计时脚本
脚本代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #!/usr/bin/env python # -*- coding: utf-8 -*- from colorama import init, Fore from zhdate import ZhDate import datetime def get_week_day(date): week_day_dict = { 0 : '星期一' , 1 : '星期二' , 2 : '星期三' , 3 : '星期四' , 4 : '星期五' , 5 : '星期六' , 6 : '星期天' , } day = date.weekday() return week_day_dict[day] def time_parse(today): distance_year = (datetime.datetime.strptime(f "{today.year}-01-01" , "%Y-%m-%d" ).date() - today).days distance_year = distance_year if distance_year > 0 else ( datetime.datetime.strptime(f "{today.year + 1}-01-01" , "%Y-%m-%d" ).date() - today).days distance_big_year = (ZhDate(today.year, 1 , 1 ).to_datetime().date() - today).days distance_big_year = distance_big_year if distance_big_year > 0 else ( ZhDate(today.year + 1 , 1 , 1 ).to_datetime().date() - today).days distance_4_5 = (datetime.datetime.strptime(f "{today.year}-04-05" , "%Y-%m-%d" ).date() - today).days distance_4_5 = distance_4_5 if distance_4_5 > 0 else ( datetime.datetime.strptime(f "{today.year + 1}-04-05" , "%Y-%m-%d" ).date() - today).days distance_5_1 = (datetime.datetime.strptime(f "{today.year}-05-01" , "%Y-%m-%d" ).date() - today).days distance_5_1 = distance_5_1 if distance_5_1 > 0 else ( datetime.datetime.strptime(f "{today.year + 1}-05-01" , "%Y-%m-%d" ).date() - today).days distance_5_5 = (ZhDate(today.year, 5 , 5 ).to_datetime().date() - today).days distance_5_5 = distance_5_5 if distance_5_5 > 0 else ( ZhDate(today.year + 1 , 5 , 5 ).to_datetime().date() - today).days distance_8_15 = (ZhDate(today.year, 8 , 15 ).to_datetime().date() - today).days distance_8_15 = distance_8_15 if distance_8_15 > 0 else ( ZhDate(today.year + 1 , 8 , 15 ).to_datetime().date() - today).days distance_10_1 = (datetime.datetime.strptime(f "{today.year}-10-01" , "%Y-%m-%d" ).date() - today).days distance_10_1 = distance_10_1 if distance_10_1 > 0 else ( datetime.datetime.strptime(f "{today.year + 1}-10-01" , "%Y-%m-%d" ).date() - today).days # print("距离周末: ", 5 - today.weekday()) # print("距离元旦: ", distance_year) # print("距离大年: ", distance_big_year) # print("距离清明: ", distance_4_5) # print("距离劳动: ", distance_5_1) # print("距离端午: ", distance_5_5) # print("距离中秋: ", distance_8_15) # print("距离国庆: ", distance_10_1) time_ = [ { "v_" : 5 - 1 - today.weekday(), "title" : "周末" }, # 距离周末 { "v_" : distance_year, "title" : "元旦" }, # 距离元旦 { "v_" : distance_big_year, "title" : "过年" }, # 距离过年 { "v_" : distance_4_5, "title" : "清明节" }, # 距离清明 { "v_" : distance_5_1, "title" : "劳动节" }, # 距离劳动 { "v_" : distance_5_5, "title" : "端午节" }, # 距离端午 { "v_" : distance_8_15, "title" : "中秋节" }, # 距离中秋 { "v_" : distance_10_1, "title" : "国庆节" }, # 距离国庆 ] time_ = sorted (time_, key = lambda x: x[ 'v_' ], reverse = False ) return time_ def countdown(): init(autoreset = True ) today = datetime.date.today() now_ = f "{today.year}年{today.month}月{today.day}日" week_day_ = get_week_day(today) print (f '\n\t\t {Fore.GREEN}{now_} {week_day_}' ) str_ = ''' 开始! ''' print (f '{Fore.RED}{str_}' ) time_ = time_parse(today) for t_ in time_: print (f '\t\t {Fore.RED}距离{t_.get("title")}还有: {t_.get("v_")}天' ) tips_ = ''' 结束! print (f '{Fore.RED}{tips_}' ) print (f '\t\t{Fore.CYAN} 公众号:AllTests软件测试\n' ) if __name__ = = '__main__' : countdown() |
在PyCharm上执行结果:
在终端上执行结果:
到此这篇关于Python统计节假日剩余天数的脚本的文章就介绍到这了,更多相关Python统计节假日内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
Python Paramiko模块中exec_command()和invoke_shell()两种操作区别
invoke_shell 使用 SSH shell channel,而 exec_command 使用 SSH exec channel,本文主要介绍了Python Paramiko模块中exec_command()和invoke_shell()两种操作区别,具有一定的参考价值,感兴趣的可以了解一下2024-02-02在Anaconda3下使用清华镜像源安装TensorFlow(CPU版)
这篇文章主要介绍了在Anaconda3下使用清华镜像源安装TensorFlow(CPU版),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-04-04pycharm运行程序时出现Run‘python tests for XXX.py‘问题及
这篇文章主要介绍了pycharm运行程序时出现Run ‘python tests for XXX.py‘问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-08-08python栈的基本定义与使用方法示例【初始化、赋值、入栈、出栈等】
这篇文章主要介绍了python栈的基本定义与使用方法,结合实例形式分析了Python栈的初始化、赋值、入栈、出栈等相关操作技巧,需要的朋友可以参考下2019-10-10
最新评论