如何在 Python 中创建当月星期几的列表?

How to create a list of days of the week of the current month in Python?

我是 Python 的新手,我正在开发我的第三个项目,一个在 Excel 中使用 Python 的日历生成器。所以我坚持创建一个函数,该函数将 return 列出当月的工作日 [星期一、星期二、星期三...]。我想也许我可以使用 for 循环和切片来做到这一点,但是它不起作用,很可能我需要使用日期时间和日历模块。

这是我现在拥有的:

l1 = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

def weekdays(start_day, weeks_in_month):
    weekdays_list = []
    for days in range(weeks_in_month):
        weekdays_list.append(l1[start_day:])
    return weekdays_list

如果您能以最基本的方式提供有关如何执行此操作的想法,我将不胜感激。

import calendar
print calendar.monthcalendar(2013, 4)
[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 0, 0, 0, 0, 0]]
import itertools

# python naming convention uses UPPERCASE for constants
WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday", "Sunday"]

# let's use number of days instead of weeks so we can handle
# fractional weeks
def weekdays(start_day, num_days):
    # create a cycling iterator to simplify wrapping around weeks
    day = itertools.cycle(WEEKDAYS)

    # skip the iterator forward to start_day
    for _ in range(WEEKDAYS.index(start_day)):
        next(day)

    # generate the list of days using a list comprehension
    return [next(day) for _ in range(num_days)]

itertools.cycle