在Python(2.7)中按周存储工作日?

2024-09-28 22:19:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我如何创建一个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,我编写了以下代码来计算两天之间的工作日数:

^{pr2}$

但是这些代码还不够。。我需要知道每个月的哪个星期有多少个工作日。在

编辑:我的操作系统是Windows7。我使用的是python2.7。在


Tags: 代码脚本编辑so视觉月份august工作日
3条回答
import calendar
import collections
from datetime import date

def get_weeks(year, month, holidays):
    cal = calendar.Calendar(0)
    weeks = collections.defaultdict(lambda: 0)
    for i, week in enumerate(cal.monthdatescalendar(year, month)):
        # Get just mon-fri
        for day in week[:-2]:
            if day.month == month and day not in holidays:
                weeks['week%s' % (i+1)] += 1
    return weeks
holidays = [date(2013, 8, 2), date(2013, 8, 6)]
print get_weeks(2013, 8, holidays)

这给了我:

^{pr2}$

如果我在8月29日再增加一个假期,我将得到:

>>> holidays = [date(2013, 8, 2), date(2013, 8, 6), date(2013, 8, 29)]
>>> print  get_weeks(2013, 8, holidays)
{'week2': 4, 'week1': 1, 'week4': 5, 'week3': 5, 'week5': 4})

更新:

import calendar
import collections
from datetime import date

def get_weeks(year, month, holidays):
    cal = calendar.Calendar(0)
    weeks = collections.defaultdict(list)
    for i, week in enumerate(cal.monthdatescalendar(year, month)):
        # Get just mon-fri
        for day in week[:-2]:
            if day.month == month and day not in holidays:
                weeks['week%s' % (i+1)].append(day)
    return weeks
holidays = [date(2013, 8, 2), date(2013, 8, 6), date(2013, 8, 29)]
print get_weeks(2013, 8, holidays)

这给了我:

{
  'week1': [date(2013, 8, 1)],
  'week2': [date(2013, 8, 5), date(2013, 8, 7), date(2013, 8, 8),
            date(2013, 8, 9)],
  'week3': [date(2013, 8, 12), date(2013, 8, 13), date(2013, 8, 14),
            date(2013, 8, 15), date(2013, 8, 16)],
  'week4': [date(2013, 8, 19), date(2013, 8, 20), date(2013, 8, 21),
            date(2013, 8, 22), date(2013, 8, 23)],
  'week5': [date(2013, 8, 26), date(2013, 8, 27), date(2013, 8, 28),
            date(2013, 8, 30)]

}

我不知道你到底想怎样存储数据,但这就是你要找的吗?在

August_2013 = {}

def store(month, week, date):
    if week in month and date not in month[week]:
        month[week].append(date)
    else:
        month[week] = [date]

store(August_2013, "week1", "8/1/2013")
store(August_2013, "week1", "8/2/2013")
store(August_2013, "week2", "8/5/2013")

#calling August_2013 then returns {'week1': ['8/1/2013', '8/2/2013'], 'week2': ['8/5/2013']}

要访问你的数据,你可以这样做:

^{pr2}$

假设您的“一周”在周日结束,工作日是周一到周五(基于您的示例)

import calendar
from datetime import date

def get_working_days(year, month, holidays):
  first_day, last_day = calendar.monthrange(year, month) # this gives the weekday of the first day of the month followed by the last day

  week = 1
  if first_day > 4: week = 0 # Assuming if you have Saturday, Sunday at the beginning of the month this doesn't start as the first week.

  working_days = {}

  for day in range(1, last_day+1):
    weekstr = 'week{0}'.format(week)
    if week > 0 and not weekstr in working_days:
      working_days[weekstr] = []
    today = date(year,month,day)
    if today.weekday() < 5:
      if today not in holidays:
        working_days[weekstr].append(today.strftime('%m/%d/%Y'))
    if today.weekday() == 6:
      week += 1

  return working_days

holidays = [date(2013, 8, 2), date(2013, 8, 6)]
print get_working_days(2013, 8, holidays)

退货

^{pr2}$

相关问题 更多 >