python - 如何在Python中迭代开始日期和结束日期之间的所有日子
问题描述
使用 Python,我从一个需要日、月和年输入的函数调用。
客观的
我正在提取从 10 月到 4 月运行的篮球数据,因此每天都有大量数据。我希望能够遍历从 2013 年 10 月 1 日到 2020 年 8 月 1 日的每一天/每一月和每一年。
client.team_box_scores(
day=, month=2, year=2017,
output_type=OutputType.CSV,
output_file_path="./1_1_2017_box_scores.csv"
)
初步研究
通过研究,我发现了datetime模块以及我可以利用的for 循环,但是就将它们嵌入到上述函数中而言,我真的不知道该怎么做。
先感谢您。
解决方案
import datetime
start_date = datetime.date(year=2013, month=10, day=1)
end_date = datetime.date(year=2020, month=8, day=1)
current_date = start_date
# Iterating over all dates from start date until end date including end date ("inclusive")
while current_date <= end_date:
# Calling the function that you need, with the appropriate day-month-year combination
# Outputting to path that is build based on current day-month-year combination
client.team_box_scores(
day=current_date.day, month=current_date.month, year=current_date.year,
output_type=OutputType.CSV,
output_file_path=f"./{current_date.day}_{current_date.month}_{current_date.year}_box_scores.csv"
)
# Advancing current date by one day
current_date += datetime.timedelta(days=1)
推荐阅读
- leaflet - 如何在传单中添加 maidengrid 叠加层
- javascript - JSX 中的 lambda 是如何工作的?
- c# - 如何创建下载文件并刷新页面
- python-3.x - 在指定存储库中打开新 PR 后,如何自动接收 PR 状态(标题、开启者等)?
- wordpress - 测试电子邮件已发送但未由 WordPress 交付
- vim - 尝试将文件类型添加到 autocmd 会产生错误
- delphi - 如何在 Delphi 2010 中运行媒体播放器时修复“初始化 MCI 时出现问题”调试消息
- c# - 为什么我的 Enum 参数回到默认值?
- c# - Owin 新项目抛出 NullReference
- vb.net - 如何在 VB.NET 的 Excel 工作表中设置密码?