python判断工作日,节假日

python判断工作日,节假日

  • 模块 chinesecalendar
  • 爬虫式的方法
  • 模块 pandas
  • 实例
  • 模块 chinesecalendar

    针对中国的节假日,强烈推荐。
    https://pypi.org/project/chinesecalendar/

    哪怕日期属于周一到周五的某一天,但它属于中国的节假日,就会判断这一天为休息日。比如元旦,春节,清明节之类的节假日。同理,如果日期属于周末,但它是调休的,那这一天就是工作日。

    # 导入模块
    import datetime
    from chinese_calendar import is_workday
    
    # 获取现在的时间
    date = datetime.datetime.now().date()
    if is_workday(date):
        print('工作日')
    else:
        print('休息日')
    
    # 指定时间
    date = datetime.datetime(2022, 1, 31)
    if is_workday(date):
      print('工作日')
    else:
      print('休息日')
    

    爬虫式的方法

    用Requests请求,上网查询。

    import json
    import requests
    import time
    #自行设置格式 格式20211224
    nowTime = time.strftime('%Y%m%d', time.localtime())
    d = '20211224'
    # 节假日接口(工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2 )
    server_url = "http://www.easybots.cn/api/holiday.php?d="
    
    req = requests.get(server_url + d)
    
    # 获取data值
    vop_data = json.loads(req.text)
    print('日期 ' + str(d) + '\n查询结果为 ' + str(vop_data) + '\n结论 ', end=' ')
    if int(vop_data[d]) == 0:
        print('工作日')
    elif int(vop_data[d]) == 1:
        print('是周末')
    elif int(vop_data[d]) == 2:
        print('节假日')
    else:
        print('Error')
    

    模块 pandas

    这个对于日期的判断使用了西方的标准。而这个模块是用于输出,不是判断。
    date_range() 函数是pandas里的一个方法,能通过调整参数输出想要的结果。pandas和第一个连用效果会好很多

    
    import pandas as pd
    day = pd.date_range(start='2022-1-1',end='2022-1-31')                 # 完整的31天
    weekday = pd.date_range(start='2022-1-1',end='2022-1-31', freq='B')   # 31天中的工作日
    weekend = [x for x in day if x not in weekday]                        # 31天中的非工作日
    
    print(day)
    print(weekday)
    print(weekend)
    
    
    
    ''' 输出结果
    DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
                   '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
                   '2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12',
                   '2022-01-13', '2022-01-14', '2022-01-15', '2022-01-16',
                   '2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20',
                   '2022-01-21', '2022-01-22', '2022-01-23', '2022-01-24',
                   '2022-01-25', '2022-01-26', '2022-01-27', '2022-01-28',
                   '2022-01-29', '2022-01-30', '2022-01-31'],
                  dtype='datetime64[ns]', freq='D')
    DatetimeIndex(['2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06',
                   '2022-01-07', '2022-01-10', '2022-01-11', '2022-01-12',
                   '2022-01-13', '2022-01-14', '2022-01-17', '2022-01-18',
                   '2022-01-19', '2022-01-20', '2022-01-21', '2022-01-24',
                   '2022-01-25', '2022-01-26', '2022-01-27', '2022-01-28',
                   '2022-01-31'],
                  dtype='datetime64[ns]', freq='B')
    [Timestamp('2022-01-01 00:00:00', freq='D'),
     Timestamp('2022-01-02 00:00:00', freq='D'),
     Timestamp('2022-01-08 00:00:00', freq='D'),
     Timestamp('2022-01-09 00:00:00', freq='D'),
     Timestamp('2022-01-15 00:00:00', freq='D'),
     Timestamp('2022-01-16 00:00:00', freq='D'),
     Timestamp('2022-01-22 00:00:00', freq='D'),
     Timestamp('2022-01-23 00:00:00', freq='D'),
     Timestamp('2022-01-29 00:00:00', freq='D'),
     Timestamp('2022-01-30 00:00:00', freq='D')]
    '''
    
    

    实例

    确定一个时间区间,然后找出区间里哪些是工作日哪些是休息日。

    import pandas as pd
    day = pd.date_range(start='20220101',periods=40)      # 从元旦开始的连续40天
    weekday = []   # 工作日
    weekend = []   # 休息日
    
    from chinese_calendar import is_workday
    for date in day:
        if is_workday(date):
            weekday.append(date)
        else:
            weekend.append(date)
    
    for i in weekend:
        print(i.year, i.month, i.day)
    
    

    参考资料:
    https://www.cnblogs.com/insane-Mr-Li/p/15378925.html
    https://www.jianshu.com/p/1ebbfa30235f

    来源:今天多喝热水

    物联沃分享整理
    物联沃-IOTWORD物联网 » python判断工作日,节假日

    发表评论