Would anyone know how to make Python return a date format like this from the calendar function?:
date = "20170714"
Basically I am looking for some enhancements if its possible for the API request to gather hourly weather data.. My problem is the code needs to be re-ran for each day.. Its a little clunky and time consuming to rerun the script 365 times to get a years worth of weather data, but its the only way I know how. Can a loop with the calendar function automate things?
from urllib.request import urlopen
from pandas.io.json import json_normalize
import json
import pandas as pd
api_key = ""
date = "20170714"
zip_code = "96345"
response = urlopen("http://api.wunderground.com/api/%s/history_%s/q/%s.json" % (api_key, date, zip_code))
json_data = response.read().decode('utf-8', 'replace')
data = json.loads(json_data)
for observation in data['history']['observations']:
print("Date/Time: " + observation['date']['pretty'])
print("Temperature: " + observation['tempi'])
print("Humidity: " + observation['hum'])
df = json_normalize(data['history']['observations'])
df = df[['date.pretty','tempi','hum']]
df['date.pretty'] = pd.to_datetime(df['date.pretty'])
print(df)
This simple calendar script can return every day in the month of December 2013, but its not in a format that weather underground API would understand... Ultimately I am hoping to create a loop where the API request can gather a months data, (or even an entire year at a time) with the calendar function Vs one day manual fashion.... Is this possible??!
import calendar
tc = calendar.TextCalendar(firstweekday=0)
for i in tc.itermonthdays(2013,12):
print(i)
Thanks