I need help with saving data I read with API key to csv. The code I have is below:
import requests
import pandas as pd
def get_precip(gooddate):
urlstart = 'http://api.wunderground.com/api/API_KEY/history_'
urlend = '/q/Switzerland/Zurich.json'
url = urlstart + str(gooddate) + urlend
data = requests.get(url).json()
for summary in data['history']['dailysummary']:
abc = ','.join((gooddate,summary['date']['year'],summary['date']['mon'],summary['date']['mday'],summary['precipm'], summary['maxtempm'], summary['meantempm'],summary['mintempm']))
df = pd.DataFrame(data=abc)
df.to_csv('/home/user/Desktop/2013_weather.csv', index=False)
if __name__ == "__main__":
from datetime import date
from dateutil.rrule import rrule, DAILY
a = date(2013, 1, 1)
b = date(2013, 12, 31)
for dt in rrule(DAILY, dtstart=a, until=b):
get_precip(dt.strftime("%Y%m%d"))
I'm sure that can't work this way, because it need to be saved into some list or dictionary before transform into dataframe, but not sure how to do that this time. If save it to the list it will give me just one row? Any help is welcomed. Thanks.