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?

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

print start.strftime('%Y%m%d') and start += datetime.timedelta(days=1) are inner loop.
Awesome @jonrsharpe, just what I was looking for!
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.