Skip to content

Instantly share code, notes, and snippets.

@Laisky
Created October 8, 2018 03:30
Show Gist options
  • Save Laisky/7e00faedadd7b35b18da6958824636c6 to your computer and use it in GitHub Desktop.
Save Laisky/7e00faedadd7b35b18da6958824636c6 to your computer and use it in GitHub Desktop.
从 XMLS 中获取连续签到日期超过 7 天的 id。
from datetime import datetime
import glob
from collections import defaultdict
import xlrd
LEAST_CONT_DAYS = 7
def load_all_files():
return glob.glob('/Users/laisky/Downloads/data/*.xlsx')
def runner(fpath):
print("-------------------------------------------------")
print(f">> {fpath}")
print("-------------------------------------------------")
docu = xlrd.open_workbook(fpath)
sh = docu.sheet_by_index(0)
ret = defaultdict(lambda: 0)
all_vins = {}
for rx in range(sh.nrows):
date, vin,*_ = sh.row(rx)
if '签到' in date.value:
continue
date = datetime.strptime(date.value[:10],'%Y-%m-%d')
vin = vin.value.strip()
if vin not in all_vins:
all_vins[vin] = {
'last_date': date,
'cnt': 1,
}
elif (date-all_vins[vin]['last_date']).days==1:
all_vins[vin]['last_date'] = date
all_vins[vin]['cnt'] += 1
if all_vins[vin]['cnt'] >= LEAST_CONT_DAYS:
if ret[vin] < all_vins[vin]['cnt']:
ret[vin] = all_vins[vin]['cnt']
else:
all_vins[vin] = {
'last_date': date,
'cnt': 1,
}
for k,v in sorted(ret.items(), key=lambda kv: kv[1], reverse=True):
print(f"{k}: {v}")
def main():
for fpath in load_all_files():
runner(fpath)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment