For loop with Python - using ints instead of datetime -
i have datascience task requires getting historical weather data. have looked @ wunderground.com api example structured so:
http://api.wunderground.com/api/d23ac65706dbf6dd/history_yyyymmdd/q/ca/san_francisco.json
therefore, when attempting build dataset need first day of year last day of year, can't with:
20140101 20140102 20140103 ... 20141229 20141230 20141231
the thing can come is:
for m in range(1, 13): d in range(1, 32): r = requests.get("http://api.wunderground.com/api/0def10027afaebb7/history_2014'+str(m)+'/'+str(d)+'/q/mexico/mexico_city.json") data = r.json()
but won't work. how go in python?
here minimal example demonstrates how iterate on actual dates:
>>> import datetime >>> start = datetime.date(2014, 1, 1) >>> end = datetime.date(2014, 1, 5) >>> while start <= end: print start.strftime('%y%m%d') start += datetime.timedelta(days=1) 20140101 20140102 20140103 20140104 20140105
see the datetime
documentation more information.
Comments
Post a Comment