python區(qū)分節(jié)假日、工作日畦粮、周末

由于工作需求散址,要對(duì)幾萬的數(shù)據(jù)的日期區(qū)分節(jié)假日、工作日和周末三種數(shù)據(jù)宣赔。下面是我的心得與代碼预麸,有什么問題可以盡管提出來哦。

方法一:

首先儒将,先放一種訪問url的代碼吏祸,這個(gè)方法其實(shí)很方便,不需要自行手動(dòng)輸入太多钩蚊。

import json
import requests
import time
#自行設(shè)置格式 格式20190225
nowTime = time.strftime('%Y%m%d', time.localtime())
d = '20190720'
# 節(jié)假日接口(工作日對(duì)應(yīng)結(jié)果為 0, 休息日對(duì)應(yīng)結(jié)果為 1, 節(jié)假日對(duì)應(yīng)的結(jié)果為 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查詢結(jié)果為 ' + str(vop_data) + '\n結(jié)論 ', 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')

但是有個(gè)缺點(diǎn)就是:如果你要查詢的日期很多贡翘,你就會(huì)頻繁訪問網(wǎng)站。砰逻。鸣驱。。會(huì)很容易被網(wǎng)站認(rèn)為你攻擊網(wǎng)站蝠咆。踊东。然后出錯(cuò)。刚操。闸翅。。菊霜。坚冀。。像我占卧。遗菠。联喘。华蜒。。豁遭。叭喜。。我的數(shù)據(jù)是差不多6萬這樣蓖谢,訪問到3萬次的時(shí)候捂蕴。譬涡。報(bào)錯(cuò)了(如下)

ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '遠(yuǎn)程主機(jī)強(qiáng)迫關(guān)閉了一個(gè)現(xiàn)有的連接。', None, 10054, None))

所以當(dāng)你要判斷的日期數(shù)量不多的時(shí)候啥辨,就可以使用方法一涡匀、當(dāng)你判斷數(shù)量太多時(shí),這個(gè)方法就不太可行了溉知。當(dāng)然陨瘩,有些朋友會(huì)說,我可以設(shè)置間隔级乍,還可以加什么加什么舌劳。。玫荣∩醯可能是我比較菜。捅厂。我也嘗試過增加某些代碼贯卦。”捍可是還是不成功脸侥,關(guān)于間隔,我沒有去嘗試盈厘,就開始寫方法二的代碼了睁枕。

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

方法二

方法二比較笨拙。沸手。因?yàn)槲艺也坏胶玫姆椒ㄍ庥觯荒茏约喝ゲ樵內(nèi)掌谧约簩懘a了。契吉。跳仿。發(fā)現(xiàn)更好方法的大神煩請(qǐng)你在下面留言、捐晶。菲语。跪求簡(jiǎn)潔明了的方法哈哈哈。

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 

在倒數(shù)第7行那里惑灵,要把自己的日期改成上面的形式山上,下面舉個(gè)例子。假設(shè)我的日期格式是09/24/2018

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

自己查詢真的是有點(diǎn)麻煩的英支。佩憾。不知道能不能找到更好的方法哈哈哈。有更好的方法在下面留言哦!M薄@慊啤!萬分感謝

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末抡驼,一起剝皮案震驚了整個(gè)濱河市鬼廓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌致盟,老刑警劉巖桑阶,帶你破解...
    沈念sama閱讀 211,123評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異勾邦,居然都是意外死亡蚣录,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門眷篇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來萎河,“玉大人,你說我怎么就攤上這事蕉饼∨氨” “怎么了?”我有些...
    開封第一講書人閱讀 156,723評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵昧港,是天一觀的道長(zhǎng)擎椰。 經(jīng)常有香客問我,道長(zhǎng)创肥,這世上最難降的妖魔是什么达舒? 我笑而不...
    開封第一講書人閱讀 56,357評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮叹侄,結(jié)果婚禮上巩搏,老公的妹妹穿的比我還像新娘。我一直安慰自己趾代,他們只是感情好贯底,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評(píng)論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著撒强,像睡著了一般禽捆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上飘哨,一...
    開封第一講書人閱讀 49,760評(píng)論 1 289
  • 那天胚想,我揣著相機(jī)與錄音,去河邊找鬼杖玲。 笑死顿仇,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的摆马。 我是一名探鬼主播臼闻,決...
    沈念sama閱讀 38,904評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼囤采!你這毒婦竟也來了述呐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,672評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤蕉毯,失蹤者是張志新(化名)和其女友劉穎乓搬,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體代虾,經(jīng)...
    沈念sama閱讀 44,118評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡进肯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了棉磨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片江掩。...
    茶點(diǎn)故事閱讀 38,599評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖乘瓤,靈堂內(nèi)的尸體忽然破棺而出环形,到底是詐尸還是另有隱情,我是刑警寧澤衙傀,帶...
    沈念sama閱讀 34,264評(píng)論 4 328
  • 正文 年R本政府宣布抬吟,位于F島的核電站,受9級(jí)特大地震影響统抬,放射性物質(zhì)發(fā)生泄漏火本。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評(píng)論 3 312
  • 文/蒙蒙 一聪建、第九天 我趴在偏房一處隱蔽的房頂上張望发侵。 院中可真熱鬧,春花似錦妆偏、人聲如沸刃鳄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叔锐。三九已至,卻和暖如春见秽,著一層夾襖步出監(jiān)牢的瞬間愉烙,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評(píng)論 1 264
  • 我被黑心中介騙來泰國打工解取, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留步责,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,286評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像蔓肯,于是被迫代替她去往敵國和親遂鹊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評(píng)論 2 348