Use a part of an output as input

59 views Asked by At

I am looking for something i am not able to fix:

I have a code that is loading data via an API andreturns the last known price of Bitcoin, and other information. The code is :

resp = requests.get('https://www.alphavantage.co/query', params={
    'function': 'CURRENCY_EXCHANGE_RATE',
    'from_currency': 'XRP', 
    'to_currency': 'EUR',
    'apikey': AV_API_KEY
})
resp

resp.json()

When I execute the code, I have this output :

{'Realtime Currency Exchange Rate': {'1. From_Currency Code': 'BTC',
   '2. From_Currency Name': 'Bitcoin',
   '3. To_Currency Code': 'EUR',
   '4. To_Currency Name': 'Euro',
   '5. Exchange Rate': '14322.63532',
   '6. Last Refreshed': '2020-11-27 22:59:05',
   '7. Time Zone': 'UTC',
   '8. Bid Price': '0.46113245',
   '9. Ask Price': '0.46117422'}}

I want to use only the Exchange Rate value (14322.63532) to then compare it to the Exchange Rate two minutes later, to know the % variation.

Do someone have any clue to how I can isolate the Exchange Rate of the rest of the output ?

Thank you !

EDIT :

I tried this


newdict = {} 
newdict['Realtime Currency Exchange Rate']['5. Exchange Rate'] 

but i have KeyError: 'Realtime Currency Exchange Rate'

1

There are 1 answers

4
Barmar On

newdict needs to be the output you showed, not an empty dictionary.

newdict = {'Realtime Currency Exchange Rate': {'1. From_Currency Code': 'BTC',
   '2. From_Currency Name': 'Bitcoin',
   '3. To_Currency Code': 'EUR',
   '4. To_Currency Name': 'Euro',
   '5. Exchange Rate': '14322.63532',
   '6. Last Refreshed': '2020-11-27 22:59:05',
   '7. Time Zone': 'UTC',
   '8. Bid Price': '0.46113245',
   '9. Ask Price': '0.46117422'}
}
print(float(newdict['Realtime Currency Exchange Rate']['5. Exchange Rate']))