0

So I have tried different methods of returning the data using data from a JSON file running in a loop.

The idea is that I have a config.json file with ip`s that need to be feed to the function when its called upon.

{
    "ip1" : "10.0.0.111",
    "ip2" : "10.0.0.112"
  }
import json
import urllib.request

with open('config.json') as config_file:
    data = json.load(config_file)

def temprature(v):

    urlData = f"http://{v}:8080/getdevice?device=type28_1"
    #print(urlData)
    webURL = urllib.request.urlopen(urlData)
    data = webURL.read()
    encoding = webURL.info().get_content_charset('utf-8')
    tempData = json.loads(data.decode(encoding))
    return tempData["Celsius"]


for (k, v) in data.items():
   #print("Key: " + k)
   temprature(v)
   #print(str(v))

I can`t see or figure out how I can fetch the tempdata and save it to an external variable. I have tried to make a variable that calls the for loop but that failed for me as well.

Edit: Being referred to as a duplicate of This post. But this does not cover the return from a for loop.

Christophermp
  • 176
  • 1
  • 3
  • 13
  • 2
    What is your expected output? This code doesn't make much sense, because you return something from the function but don't assign it to anything in the calling code, so it's discarded. – ggorlen Aug 07 '19 at 19:38
  • `temp = temprature(v)`? You call the function but don't do anything with the result. – Alexander Aug 07 '19 at 19:39
  • Most commonly people create an empty list and `append` to it in the loop, but you haven't told us how you need the result stored – G. Anderson Aug 07 '19 at 19:39
  • My expected output is 20.75 as the JSON data I read is: {"Address":"ff00000a70d57128","Celsius":20.75,"Fahrenheit":69.35} It all works if I hardcode the ip and call the function with: var = tempdata() – Christophermp Aug 07 '19 at 19:41
  • So you just want one number even though you're making 2 requests in a loop? This doesn't make a lot of sense to me. – ggorlen Aug 07 '19 at 19:45
  • Let me clarify, what i am trying to do is to fetch the values one by one reading my config.json so i can add several other ip`s to the list. – Christophermp Aug 07 '19 at 19:49
  • how do you want the output structured? Do you just want a list of values? Do you want key/value pairs like in a dictionary? – mauve Aug 07 '19 at 19:51
  • after return tempData["Celsius"] i would have a value stored to a VAR. What i am trying to achive is an out put of 23 43 Or whatever it returns upon called. – Christophermp Aug 07 '19 at 19:53
  • 1
    `for v in data.values(): print(temprature(v))` doesn't do that? Or adding it to a data structure as mauve suggests below, then `print(temperature_data)`? – ggorlen Aug 07 '19 at 19:54
  • Thanks! That solved my issue! – Christophermp Aug 07 '19 at 19:57

1 Answers1

3

You need to save the value to some data structure - list, dictionary, whatever:

temperature_data = []
for (k, v) in data.items():
   #print("Key: " + k)
   temperature_data.append(temprature(v))
   #print(str(v))
    print(temprature(v))

print(temperature_data)

temperature_data = {}
for (k, v) in data.items():
   #print("Key: " + k)
   temperature_data[k] = temprature(v)
   #print(str(v))
   print(temprature(v))

print(temperature_data)

as @ggorlen mentioned, it seems that printing the results is what you'd like to ultimately do, so I added a print statement in the loop.

Make sure to establish the data structure outside of the loop, because otherwise, you'll be writing over the variable on each loop.

mauve
  • 2,707
  • 1
  • 20
  • 34