Iterating through a range of dates in Python

后端 未结 23 1328
醉酒成梦
醉酒成梦 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:10

    Using pendulum.period:

    import pendulum
    
    start = pendulum.from_format('2020-05-01', 'YYYY-MM-DD', formatter='alternative')
    end = pendulum.from_format('2020-05-02', 'YYYY-MM-DD', formatter='alternative')
    
    period = pendulum.period(start, end)
    
    for dt in period:
        print(dt.to_date_string())
    

提交回复
热议问题