0

I want to get a list that contains the exchange rate for USD.

This displays the data inside the API for reference.

def ge(url):
 response = urllib.request.urlopen(url)
 content_str = response.read()
 content = json.loads(content_str)
 return json.dumps(content)
print (ge(url))

And I'm having trouble with this part of the code where I can't get the exchange rate for USD. My other problem is that when I get rid of 'base in i', I get a error message saying the following, "string indices must be integers".

import json
import urllib.request
url = "https://api.exchangeratesapi.io/latest"
def get_response(url):
 response = urllib.request.urlopen(url)
 content_str = response.read()
 content = json.loads(content_str)
 outList = []
 for i in content:
 if('rates' in i and 'base' in i):
 innerList = []
 innerList.append(float(i["USD"]))
 outList.append(innerList)
 return json.dumps(outList)
print (get_response(url)) 

Expected: 1.1218

Actual result: I keep getting a empty bracket with nothing in it.

martineau
124k29 gold badges180 silver badges317 bronze badges
asked May 2, 2019 at 2:19
10
  • It's impossible to know what is content without reading API doc because it depends on returned JSON, if you'd like to make the post helpful, post your sample data. Commented May 2, 2019 at 2:27
  • Given that error message, at least one element in content must be a plain string, and you can't index a string with i["USD"]. Commented May 2, 2019 at 2:28
  • 2
    content['base']['USD'] is what you want. You should brush up on the basics as you seem to have no idea what a string or a dictionary is. Commented May 2, 2019 at 2:31
  • 1
    use print(content, type(content) ) to see what you have. If it is list then you can check print( content[0], type(content[0]) ). If it is dictionary then you can check print( content.keys() ), etc. Commented May 2, 2019 at 2:42
  • 1
    @ShioT actually, it's content['rates']['USD']. The value for the key base is 'EUR'. Commented May 2, 2019 at 2:55

2 Answers 2

2

The urllib GET request is merely returning a dictionary. We see it contains three keys: base (whose value is 'EUR'), rates (whose value is a dictionary containing conversion rates to the other currencies), and date (whose value is the current date). You want to get the conversion rate of USD. We therefore access the rates key with content['rates']. This returns a dictionary with keys being currency names ('GBP', 'USD', etc.) whose values are the amount of that currency that is required to equal 1 of the base. To access the value of the USD entry, we do content['rates']['USD'], which returns the desired result of 1.1218 USD per euro. These are basic python concepts, so be sure to know them before moving on to more advanced projects.

Revised code:

def get_response(url):
 response = urllib.request.urlopen(url)
 content_str = response.read()
 content = json.loads(content_str)
 return content['rates']['USD']
answered May 2, 2019 at 2:54
1

Tips:You can addprint(content) after the line content=json.loads(content_str),and then you could see the type of content, it's a nested Dictionary. @101arrowZ's code works well. We don't need to check whether the keys rates and base are in the dictionary, for the 'check' part is contained in content['rates']['USD']. If they are in content, the value may be returned; otherwise, return none.

answered May 2, 2019 at 11:27
2
  • Ahh.. I see what you mean, I will try that. Thank you. Commented May 2, 2019 at 15:54
  • 1
    You're welcome! I'm very glad to help. Your question was my first answer on stackoverflow. What I told you above was the process of how I got the answer of your question. I ran the code on my computer and it worked. Commented May 2, 2019 at 21:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.