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.
2 Answers 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']
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.
-
Ahh.. I see what you mean, I will try that. Thank you.swiftly– swiftly05/02/2019 15:54:31Commented May 2, 2019 at 15:54
-
1You'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.Juliecodestack– Juliecodestack05/02/2019 21:57:49Commented May 2, 2019 at 21:57
Explore related questions
See similar questions with these tags.
content
without reading API doc because it depends on returned JSON, if you'd like to make the post helpful, post your sample data.content
must be a plain string, and you can't index a string withi["USD"]
.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.print(content, type(content) )
to see what you have. If it is list then you can checkprint( content[0], type(content[0]) )
. If it is dictionary then you can checkprint( content.keys() )
, etc.content['rates']['USD']
. The value for the keybase
is 'EUR'.