python 生成日期序列
时间: 2023-07-04 15:26:11 浏览: 91
你可以使用Python中的datetime和timedelta模块来生成日期序列。下面是一个例子,生成从2021-01-01到2021-01-31的日期序列:
```python
from datetime import datetime, timedelta
start_date = datetime(2021, 1, 1)
end_date = datetime(2021, 1, 31)
date_list = []
while start_date <= end_date:
date_list.append(start_date.date())
start_date += timedelta(days=1)
print(date_list)
```
输出结果为:
```
[datetime.date(2021, 1, 1), datetime.date(2021, 1, 2), datetime.date(2021, 1, 3), datetime.date(2021, 1, 4), datetime.date(2021, 1, 5), datetime.date(2021, 1, 6), datetime.date(2021, 1, 7), datetime.date(2021, 1, 8), datetime.date(2021, 1, 9), datetime.date(2021, 1, 10), datetime.date(2021, 1, 11), datetime.date(2021, 1, 12), datetime.date(2021, 1, 13), datetime.date(2021, 1, 14), datetime.date(2021, 1, 15), datetime.date(2021, 1, 16), datetime.date(2021, 1, 17), datetime.date(2021, 1, 18), datetime.date(2021, 1, 19), datetime.date(2021, 1, 20), datetime.date(2021, 1, 21), datetime.date(2021, 1, 22), datetime.date(2021, 1, 23), datetime.date(2021, 1, 24), datetime.date(2021, 1, 25), datetime.date(2021, 1, 26), datetime.date(2021, 1, 27), datetime.date(2021, 1, 28), datetime.date(2021, 1, 29), datetime.date(2021, 1, 30), datetime.date(2021, 1, 31)]
```
阅读全文