如何创建一个 Python 脚本来存储工作日日期以及任何月份的相应周数?
这个问题令人困惑,所以这是一个“视觉”:
August_2013 = {
( week1 : (8/1/2013), (8/2/2013) ),
( week2 : (8/5/2013), (8/6/2013), (8/7/2013), (8/8/2013), (8/9/2013) ),
( week3 : (8/12/2013), (8/13/2013), (8/14/2013), (8/15/2013), (8/16/2013) ),
( week4 : (8/19/2013), (8/20/2013), (8/21/2013), (8/22/2013), (8/23/2013) ),
( week5 : (8/26/2013), (8/27/2013), (8/28/2013), (8/29/2013), (8/30/2013) )
}
通过搜索整个 SO,我编写了以下代码来计算两天之间的工作日数:
def businessDays(start_date, end_date):
day_generator = (start_date + timedelta(x + 1) for x in xrange((end_date - start_date).days))
holidays = [ date(2013, 7, 4), date(2013, 12, 25) ] # Will add more later
return sum(1 for day in day_generator if day.weekday() < 5 and day not in holidays)
但是这个代码还不够。我需要知道任何给定月份的哪一周有多少个工作日。
编辑:我的操作系统是 Windows 7。我使用的是 Python 2.7。