我会考虑dateutil.rrule
(pip install python-dateutil
):
import datetime
from dateutil import rrule
start = datetime.datetime(2013, 9, 1)
end = datetime.datetime(2013, 9, 30)
rule = rrule.rrule(dtstart=start, freq=rrule.DAILY,
byweekday=[rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR]
)
rule.between(start, end, inc=True)
[datetime.datetime(2013, 9, 2, 0, 0),
datetime.datetime(2013, 9, 3, 0, 0),
datetime.datetime(2013, 9, 4, 0, 0),
datetime.datetime(2013, 9, 5, 0, 0),
datetime.datetime(2013, 9, 6, 0, 0),
datetime.datetime(2013, 9, 9, 0, 0),
datetime.datetime(2013, 9, 10, 0, 0),
datetime.datetime(2013, 9, 11, 0, 0),
datetime.datetime(2013, 9, 12, 0, 0),
datetime.datetime(2013, 9, 13, 0, 0),
datetime.datetime(2013, 9, 16, 0, 0),
datetime.datetime(2013, 9, 17, 0, 0),
datetime.datetime(2013, 9, 18, 0, 0),
datetime.datetime(2013, 9, 19, 0, 0),
datetime.datetime(2013, 9, 20, 0, 0),
datetime.datetime(2013, 9, 23, 0, 0),
datetime.datetime(2013, 9, 24, 0, 0),
datetime.datetime(2013, 9, 25, 0, 0),
datetime.datetime(2013, 9, 26, 0, 0),
datetime.datetime(2013, 9, 27, 0, 0),
datetime.datetime(2013, 9, 30, 0, 0)]