1

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,

3 Answers 3

2

IMO you need some sort of dynamic length data structure to which you can append the data inside a for loop and then access it using index.

Therefore, you can create a list and then append all the values of for lop into it as shown below:

list = []
for i in dailyTHigh:
        list.append(i['temperatureHigh'])

Now you will be able to access the values of list as shown below:

for i in range(0,len(x)):
    print x[i]

The above approach is good as you don't need to know the number of items you need to insert as you may require equal number of variables to have the values assigned to. And you can easily access the values also.

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

2 Comments

Thanks man, this did the trick although I edited it a little to get exactly what I was looking for: #Everthing above this line works as expected, I am focusing on the below code dailyTHigh = (weather['daily']['data']) list = [] for i in dailyTHigh: list.append(i['temperatureHigh']) for i in range(0,len(list)): var1 = list[0] var2 = list[1] print (var1) print (var2) etc etc with the variables
Apologies for the weird formatting in the comments, still new to the forum
1

Just to neaten up on my above comment to the accepted answer

#Everthing above this line works as expected, I am focusing on the below code

dailyTHigh = (weather['daily']['data'])
list = []

for i in dailyTHigh:
        list.append(i['temperatureHigh'])

for i in range(0,len(list)):
        var1 = list[0]
        var2 = list[1]

Saved each list iteration to a variable, I know there will always be 8 variables so this works for me

print (var1)

Just for testing gives me what I was looking for

Comments

0

IMO you can use a stack data structure and store the data in the FILO (First In Last Out) form. This way, you can also manage data in more efficient way, even it get's bigger in size (in future)

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.