Faced a situation that I can not solve.

There is a function that takes data from the site by IP. When it is called, it returns data in a tuple format, and I need data in a dictionary.

How can this problem be solved?

Function code and its call

def request_auth_by_api(command='user_info', params={}): nonce = int(round(time.time()*1000)) sync_param = {"nonce": nonce} params.update(sync_param) params = urllib.urlencode(params) H = hmac.new(api_secret, digestmod=hashlib.sha512) H.update(params) sign = H.hexdigest() headers = {"Content-type": "application/x-www-form-urlencoded", "Key": api_key, "Sign": sign} try: conn = httplib.HTTPSConnection("api.exmo.com") conn.request("POST", "/v1/{}".format(command), params, headers) response = conn.getresponse() json_obj = json.load(response) status = response.status reason = response.reason conn.close() except: json_obj = {} status = False reason = False return json_obj, status, reason response = request_auth_by_api() print (type(response)) print response 

output function in tuple format

  • are tuples pairs? - HasmikGaryaka
  • one
    Why didn't you publish the function in question? - 0xdb
  • one
    Show the function implementation - pinguin
  • @HasmikGaryaka is unchangeable lists - pinguin
  • one
    Add an example to the question, in what form the data is returned by the function, and as you need. - user239133

1 answer 1

return json_obj, status, reason replace with return json_obj[u'BTC'], status, reason

  • Thank you for the answer. Help line returned json_obj ['balances'] ['BTC'], status, reason - ALeks
  • But there is one more question. If it were necessary to take one parameter from this function, then everything would be great. But this is the main function that takes data from the site in the form of a dictionary and would like to receive data from it, also in the form of a dictionary, in order to process this data in other functions. And I get when assigned to a variable response, the data becomes a tuple. - ALeks
  • @ALeks you return three variables - a dictionary (paired JSON) and 2 lines (status and reason). Thus it turns out that the function returns a tuple consisting of three elements - a dictionary and 2 lines. I think it's more convenient to return only json_obj, and in the case of exception, return status and reason. - pinguin