Python for Moon Question.

Asked 2 years ago, Updated 2 years ago, 94 views

nStartYear = 2017
    nEndYear = 2020

    for year in range(nStartYear, nEndYear):
        for month in range(1,13):

            yyyymm = "{0}{1:0>2}".format(str(year), str(month))

            jsonData = getNatVisitor(yyyymm, national_code, ed_cd)

            print(json.dumps(jsonData,
                             indent=4, sort_keys=True,
                             ensure_ascii=False))

It's supposed to turn the For Moon from January to December How do I write code to set nStartYear to 2017 and nEndYear to August 2020 for statement?

python for

2022-09-21 22:58

1 Answers

Try using the rule module

https://dateutil.readthedocs.io/en/stable/index.html

In [1]: from dateutil.rrule import *

In [2]: list(rrule(MONTHLY, interval=1, dtstart=date(2017, 1, 1), until=date(2020, 8, 1)))                                                        
Out[2]: 
[datetime.datetime(2017, 1, 1, 0, 0),
 datetime.datetime(2017, 2, 1, 0, 0),
 datetime.datetime(2017, 3, 1, 0, 0),
 datetime.datetime(2017, 4, 1, 0, 0),
 datetime.datetime(2017, 5, 1, 0, 0),
 datetime.datetime(2017, 6, 1, 0, 0),
 datetime.datetime(2017, 7, 1, 0, 0),
 datetime.datetime(2017, 8, 1, 0, 0),
 datetime.datetime(2017, 9, 1, 0, 0),
 datetime.datetime(2017, 10, 1, 0, 0),
 datetime.datetime(2017, 11, 1, 0, 0),
 datetime.datetime(2017, 12, 1, 0, 0),
 datetime.datetime(2018, 1, 1, 0, 0),
 datetime.datetime(2018, 2, 1, 0, 0),
 datetime.datetime(2018, 3, 1, 0, 0),
 datetime.datetime(2018, 4, 1, 0, 0),
 datetime.datetime(2018, 5, 1, 0, 0),
 datetime.datetime(2018, 6, 1, 0, 0),
 datetime.datetime(2018, 7, 1, 0, 0),
 datetime.datetime(2018, 8, 1, 0, 0),
 datetime.datetime(2018, 9, 1, 0, 0),
 datetime.datetime(2018, 10, 1, 0, 0),
 datetime.datetime(2018, 11, 1, 0, 0),
 datetime.datetime(2018, 12, 1, 0, 0),
 datetime.datetime(2019, 1, 1, 0, 0),
 datetime.datetime(2019, 2, 1, 0, 0),
 datetime.datetime(2019, 3, 1, 0, 0),
 datetime.datetime(2019, 4, 1, 0, 0),
 datetime.datetime(2019, 5, 1, 0, 0),
 datetime.datetime(2019, 6, 1, 0, 0),
 datetime.datetime(2019, 7, 1, 0, 0),
 datetime.datetime(2019, 8, 1, 0, 0),
 datetime.datetime(2019, 9, 1, 0, 0),
 datetime.datetime(2019, 10, 1, 0, 0),
 datetime.datetime(2019, 11, 1, 0, 0),
 datetime.datetime(2019, 12, 1, 0, 0),
 datetime.datetime(2020, 1, 1, 0, 0),
 datetime.datetime(2020, 2, 1, 0, 0),
 datetime.datetime(2020, 3, 1, 0, 0),
 datetime.datetime(2020, 4, 1, 0, 0),
 datetime.datetime(2020, 5, 1, 0, 0),
 datetime.datetime(2020, 6, 1, 0, 0),
 datetime.datetime(2020, 7, 1, 0, 0),
 datetime.datetime(2020, 8, 1, 0, 0)]


2022-09-21 22:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.