0

I have a datascience task that requires getting historical weather data. I have looked at wunderground.com but their API example is structured like so:

http://api.wunderground.com/api/d23ac65706dbf6dd/history_YYYYMMDD/q/CA/San_Francisco.json

Therefore, when attempting to build a dataset I need to get from the first day of the year to the last day of the year, but can't do that with:

    20140101
    20140102
    20140103
    ...
    20141229
    20141230
    20141231

The only thing that I can come up with is:

for m in range(1, 13):
    for 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 that obviously won't work. How would you go about this in Python?

gwerner
  • 83
  • 1
  • 9

3 Answers3

6

Here is a minimal example that demonstrates how to iterate over 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 for more information.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
1

An alternative using calendar module:

>>> import calendar
>>> cal = calendar.Calendar()
>>> year = 2014
>>> for month in range(1,13):
...    for day in cal.itermonthdates(year, month):
...        if day.year == year:
...           print day.strftime('%Y%m%d')
... 
20140101
20140102
20140103
20140104
...
...
20141229
20141230
20141231
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
0

Instead you can use datetime to convert int to datetime format and check whether the year matches your requirement. for e.g

import datetime
s = 20140101
s_datetime = datetime.datetime.strptime(str(s), '%Y%m%d')
if s_datetime.year == 2014:
    //Do something
Pooja
  • 1,254
  • 14
  • 17