Iterating through a range of dates in Python

后端 未结 23 1594
醉酒成梦
醉酒成梦 2020-11-22 04:40

I have the following code to do this, but how can I do it better? Right now I think it\'s better than nested loops, but it starts to get Perl-one-linerish when you have a ge

23条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 05:05

    Show the last n days from today:

    import datetime
    for i in range(0, 100):
        print((datetime.date.today() + datetime.timedelta(i)).isoformat())
    

    Output:

    2016-06-29
    2016-06-30
    2016-07-01
    2016-07-02
    2016-07-03
    2016-07-04
    

提交回复
热议问题