python区分节假日、工作日、周末

由于工作需求,要对几万的数据的日期区分节假日、工作日和周末三种数据。下面是我的心得与代码,有什么问题可以尽管提出来哦。

方法一:

首先,先放一种访问url的代码,这个方法其实很方便,不需要自行手动输入太多。

import json
import requests
import time
#自行设置格式 格式20190225
nowTime = time.strftime('%Y%m%d', time.localtime())
d = '20190720'
# 节假日接口(工作日对应结果为 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('Its weekday')
elif int(vop_data[d]) == 1:
    print('Its weekend')
elif int(vop_data[d]) == 2:
    print('Its holiday')
else:
    print('Error')

但是有个缺点就是:如果你要查询的日期很多,你就会频繁访问网站。。。。会很容易被网站认为你攻击网站。。然后出错。。。。。。。像我。。。。。。。。我的数据是差不多6万这样,访问到3万次的时候。。报错了(如下)

ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))

所以当你要判断的日期数量不多的时候,就可以使用方法一、当你判断数量太多时,这个方法就不太可行了。当然,有些朋友会说,我可以设置间隔,还可以加什么加什么。。。可能是我比较菜。。我也尝试过增加某些代码。。可是还是不成功,关于间隔,我没有去尝试,就开始写方法二的代码了。

参考:https://blog.csdn.net/iamlvshijie/article/details/72630869

方法二

方法二比较笨拙。。因为我找不到好的方法,只能自己去查询日期自己写代码了。。。发现更好方法的大神烦请你在下面留言、。。跪求简洁明了的方法哈哈哈。

def date_sort(data):
    hol = {"2017-01-01", "2017-01-02", "2017-01-27", "2017-01-28", "2017-01-29", "2017-01-30", "2017-01-31", 
           "2017-02-01","2017-02-02", "2017-04-02", "2017-04-03", "2017-04-04", "2017-04-29", "2017-04-30", 
           "2017-05-01","2017-05-28", "2017-05-29", "2017-05-30", "2017-10-01", "2017-10-02", "2017-10-03", 
           "2017-10-04", "2017-10-05", "2017-10-06", "2017-10-07", "2017-10-08", "2017-12-30", "2017-12-31",
           "2018-01-01", "2018-02-15", "2018-02-16", "2018-02-17", "2018-02-18", "2018-02-19", "2018-02-20",
           "2018-02-21", "2018-04-05", "2018-04-06", "2018-04-07", "2018-04-29", "2018-04-30", "2018-05-01"
           "2018-06-16", "2018-06-17", "2018-06-18", "2018-09-22", "2018-09-23", "2018-09-24", "2018-10-01",
           "2018-10-02", "2018-10-03", "2018-10-04", "2018-10-05", "2018-10-06", "2018-10-07", "2018-12-30",
           "2018-12-31",
           "2019-01-01", "2019-02-04", "2019-02-05", "2019-02-06", "2019-02-07", "2019-02-08", "2019-02-09",
           "2019-02-10", "2019-04-05", "2019-04-06", "2019-04-07"}
    weekend = {"2017-01-07","2017-01-08","2017-01-14","2017-01-15","2017-01-21","2017-02-05","2017-02-11",
               "2017-02-12","2017-02-18","2017-02-19","2017-02-25","2017-02-26","2017-03-04","2017-03-05",
               "2017-03-11","2017-03-12","2017-03-18","2017-03-19","2017-03-25","2017-03-26","2017-04-08",
               "2017-04-09","2017-04-15","2017-04-16","2017-04-22","2017-04-23","2017-05-06","2017-05-07",
               "2017-05-13","2017-05-14","2017-05-20","2017-05-21","2017-06-03","2017-06-04","2017-06-10",
               "2017-06-11","2017-06-17","2017-06-18","2017-06-24","2017-06-25","2017-07-01","2017-07-02",
               "2017-07-08","2017-07-09","2017-07-15","2017-07-16","2017-07-22","2017-07-23","2017-07-29",
               "2017-07-30","2017-08-05","2017-08-06","2017-08-12","2017-08-13","2017-08-19","2017-08-20",
               "2017-08-26","2017-08-27","2017-09-02","2017-09-03","2017-09-09","2017-09-10","2017-09-16",
               "2017-09-17","2017-09-23","2017-09-24","2017-10-14","2017-10-15","2017-10-21","2017-10-22",
               "2017-10-28","2017-10-29","2017-11-04","2017-11-05","2017-11-11","2017-11-12","2017-11-18",
               "2017-11-19","2017-11-25","2017-11-26","2017-12-02","2017-12-03","2017-12-09","2017-12-10",
               "2017-12-16","2017-12-17","2017-12-23","2017-12-24",
               "2018-01-06","2018-01-07","2018-01-14","2018-01-13","2018-01-21","2018-01-20","2018-01-27",
               "2018-01-28","2018-02-03","2018-02-04","2018-02-10","2018-02-25","2018-03-04","2018-03-03",
               "2018-03-11","2018-03-10","2018-03-18","2018-03-17","2018-03-25","2018-03-24","2018-03-31",
               "2018-04-01","2018-04-15","2018-04-14","2018-04-22","2018-04-21","2018-05-06","2018-05-05",
               "2018-05-13","2018-05-12","2018-05-20","2018-05-19","2018-05-26","2018-05-27","2018-06-03",
               "2018-06-02","2018-06-10","2018-06-09","2018-06-24","2018-06-23","2018-06-30","2018-07-01",
               "2018-07-02","2018-07-08","2018-07-07","2018-07-15","2018-07-14","2018-07-22","2018-07-21",
               "2018-07-29","2018-07-28","2018-08-05","2018-08-04","2018-08-12","2018-08-11","2018-08-19",
               "2018-08-18","2018-08-26","2018-08-25","2018-09-02","2018-09-01","2018-09-09","2018-09-08",
               "2018-09-16","2018-09-15","2018-10-14","2018-10-13","2018-10-21","2018-10-20","2018-10-28",
               "2018-10-27","2018-11-04","2018-11-03","2018-11-11","2018-11-10","2018-11-18","2018-11-17",
               "2018-11-25","2018-11-24","2018-12-02","2018-12-01","2018-12-09","2018-12-08","2018-12-16",
               "2018-12-15","2018-12-23","2018-12-22",
               "2019-01-06","2019-01-05","2019-01-12","2019-01-13","2019-01-19","2019-01-20","2019-01-27",
               "2019-01-26","2019-02-16","2019-02-17","2019-02-23","2019-02-24","2019-03-02","2019-03-03",
               "2019-03-09","2019-03-10","2019-03-16","2019-03-17","2019-03-23","2019-03-24","2019-03-31",
               "2019-03-30","2019-04-13","2019-04-14","2019-04-20","2019-04-21","2019-04-27"}
    date_list = []
    weeke = 'weekend'
    holiday = 'holiday'
    weekday = 'weekday'
    for i in range(len(data)):
        d = data[i]
        d = year+ '-' + month+ '-' +day#按照自己的日期格式改成以上形式
        if d in hol :
            date_list.append(holiday)
        elif d in weekend :
            date_list.append(weeke)
        else :date_list.append(weekday)
    return date_list 

在倒数第7行那里,要把自己的日期改成上面的形式,下面举个例子。假设我的日期格式是09/24/2018

>>> date = '09/24/2018'
>>> d = date[6:10] + '-' + date[0:2] + '-' + date[3:5]
>>> d
'2018-09-24'

自己查询真的是有点麻烦的。。不知道能不能找到更好的方法哈哈哈。有更好的方法在下面留言哦!!!!万分感谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,542评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,596评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,021评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,682评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,792评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,985评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,107评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,845评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,299评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,612评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,747评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,441评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,072评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,828评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,069评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,545评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,658评论 2 350