[英]Use a part of an output as input
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.我有一个代码通过 API 加载数据并返回比特币的最后已知价格和其他信息。 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:当我执行代码时,我有这个 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.我想只使用汇率值 (14322.63532) 然后将其与两分钟后的汇率进行比较,以了解百分比变化。
Do someone have any clue to how I can isolate the Exchange Rate of the rest of the output?有人知道如何隔离 output 的 rest 的汇率吗?
Thank you !谢谢 !
EDIT:编辑:
I tried this我试过这个
newdict = {}
newdict['Realtime Currency Exchange Rate']['5. Exchange Rate']
but i have KeyError: 'Realtime Currency Exchange Rate'但我有 KeyError: 'Realtime Currency Exchange Rate'
newdict
needs to be the output you showed, not an empty dictionary. newdict
需要是您显示的 output ,而不是空字典。
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']))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.