由于工作需求散址,要對(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薄@慊啤!萬分感謝