I am trying to gather weather data from an API and then store that Data in a Database for use later.
I have been able to access the data and print it out using a for loop, but I would like to assign each iteration of that for loop to a variable to be stored in a different location in a database.
How would I be able to do so?
My Current Code Below:
#!/usr/bin/python3
from urllib.request import urlopen
import json
apikey="redacted"
# Latitude & longitude
lati="-26.20227"
longi="28.04363"
# Add units=si to get it in sensible ISO units
url="https://api.forecast.io/forecast/"+apikey+"/"+lati+","+longi+"?units=si"
meteo=urlopen(url).read()
meteo = meteo.decode('utf-8')
weather = json.loads(meteo)
cTemp = (weather['currently']['temperature'])
cSum = (weather['currently']['summary'])
cRain1 = (weather['currently']['precipProbability'])
cRain2 = cRain1*100
daily = (weather['daily']['summary'])
print (cTemp)
print (cSum)
print (cRain2)
print (daily)
#Everthing above this line works as expected, I am focusing on the below code
dailyTHigh = (weather['daily']['data'])
for i in dailyTHigh:
print (i['temperatureHigh'])
Gives me an output of the following:
12.76
Clear
0
No precipitation throughout the week, with high temperatures rising to 24°C on Friday.
22.71
22.01
22.82
23.13
23.87
23.71
23.95
22.94
How would I go about assigning each of the 8 High Temperatures to a different variable?
ie, var1 = 22.71 var2 = 22.01 etc
Thanks in advance,