在工作中,我们需要填写每月工资表,说明我们所教授的所有课程。每位老师在一周的每一天都教授完全相同的课程。
我想做的是允许用户输入一个月,然后创建一个代表该月的对象,然后逐日遍历该对象。如果一天是星期一,请填写该老师星期一总是教的课。
到目前为止,我已经尝试使用datetime模块。我可以找到一种输入开始日期和结束日期的方法,但是不能创建一个月,我可以通过一个用户输入来处理我想要的方法。
我希望能够进行设置,其他员工只需输入他们的姓名和月份,该程序就可以完成剩下的工作,因为这不是一个技术熟练的环境。如果有人知道可以使用datetime或其他模块来完成此操作,我将非常感激。
答案 0 :(得分:1)
dateutil.rrule
几乎正是您所需要的。该界面有点奇怪,因为源材料和主要用例是icalendar的重复规则,dateutil遵循该术语,但除此之外,它还可以轻松处理您的用例。
答案 1 :(得分:1)
您可以使用datetime和timedelta进行此操作-在开始日期的基础上增加天数:
import datetime
# 0 == Monday, 1== Tuesday .... the list is the list of lessons taught on that day
teach = {0: ["A","B"], 1:["C","D"],2:["E"],3:["A","E"], 4:["E","B","E"]}
# get a starting month
while True:
try:
month = int(input("Month [1-12]: "))
except:
continue
if 1 <= month <= 12:
break
# date to start at
start = datetime.datetime(2019,month,1)
# create all days of that month
m = [[date, teach.get(date.weekday())] # date + lessons
for date in (start + datetime.timedelta(days=n) for n in range(32)) # all days
if date.month == month] # only those that fit into the month
print()
for day in m:
print(day)
输出:
Month [1-12]: 4
[datetime.datetime(2019, 4, 1, 0, 0), ['A', 'B']]
[datetime.datetime(2019, 4, 2, 0, 0), ['C', 'D']]
[datetime.datetime(2019, 4, 3, 0, 0), ['E']]
[datetime.datetime(2019, 4, 4, 0, 0), ['A', 'E']]
[datetime.datetime(2019, 4, 5, 0, 0), ['E', 'B', 'E']]
[datetime.datetime(2019, 4, 6, 0, 0), None]
[datetime.datetime(2019, 4, 7, 0, 0), None]
[datetime.datetime(2019, 4, 8, 0, 0), ['A', 'B']]
[datetime.datetime(2019, 4, 9, 0, 0), ['C', 'D']]
[datetime.datetime(2019, 4, 10, 0, 0), ['E']]
[datetime.datetime(2019, 4, 11, 0, 0), ['A', 'E']]
[datetime.datetime(2019, 4, 12, 0, 0), ['E', 'B', 'E']]
[datetime.datetime(2019, 4, 13, 0, 0), None]
[datetime.datetime(2019, 4, 14, 0, 0), None]
[datetime.datetime(2019, 4, 15, 0, 0), ['A', 'B']]
[datetime.datetime(2019, 4, 16, 0, 0), ['C', 'D']]
[datetime.datetime(2019, 4, 17, 0, 0), ['E']]
[datetime.datetime(2019, 4, 18, 0, 0), ['A', 'E']]
[datetime.datetime(2019, 4, 19, 0, 0), ['E', 'B', 'E']]
[datetime.datetime(2019, 4, 20, 0, 0), None]
[datetime.datetime(2019, 4, 21, 0, 0), None]
[datetime.datetime(2019, 4, 22, 0, 0), ['A', 'B']]
[datetime.datetime(2019, 4, 23, 0, 0), ['C', 'D']]
[datetime.datetime(2019, 4, 24, 0, 0), ['E']]
[datetime.datetime(2019, 4, 25, 0, 0), ['A', 'E']]
[datetime.datetime(2019, 4, 26, 0, 0), ['E', 'B', 'E']]
[datetime.datetime(2019, 4, 27, 0, 0), None]
[datetime.datetime(2019, 4, 28, 0, 0), None]
[datetime.datetime(2019, 4, 29, 0, 0), ['A', 'B']]
[datetime.datetime(2019, 4, 30, 0, 0), ['C', 'D']]
Doku:
您必须删除学校停课的特殊日期的日期-仅查看工作日。