Python | Get the real time currency exchange rate
Last Updated :
14 Dec, 2018
Python is a very versatile programming language. Python is being used in almost each mainstream technology and one can develop literally any application with it.
Let’s see a Python program to the real-time currency exchange rate. To use this service, one must need the API key, which can be get form here. One can get the list of currency codes from here.
We will be using CURRENCY_EXCHANGE_RATE API
,which can return the realtime exchange rate for any pair of digital currency (e.g., Bitcoin) or physical currency (e.g., USD).
Modules Needed :
requests
json
Below is the implementation :
def RealTimeCurrencyExchangeRate(from_currency, to_currency, api_key) :
import requests, json
main_url = base_url + "&from_currency =" + from_currency +
"&to_currency =" + to_currency + "&apikey =" + api_key
req_ob = requests.get(main_url)
result = req_ob.json()
print ( " Result before parsing the json data :\n" , result)
print ( "\n After parsing : \n Realtime Currency Exchange Rate for" ,
result[ "Realtime Currency Exchange Rate" ]
[ "2. From_Currency Name" ], "TO" ,
result[ "Realtime Currency Exchange Rate" ]
[ "4. To_Currency Name" ], "is" ,
result[ "Realtime Currency Exchange Rate" ]
[ '5. Exchange Rate' ], to_currency)
if __name__ = = "__main__" :
from_currency = "USD"
to_currency = "INR"
api_key = "Your_Api_Key"
RealTimeCurrencyExchangeRate(from_currency, to_currency, api_key)
|
Output :
Result before parsing the json data :
{‘Realtime Currency Exchange Rate’: {‘1. From_Currency Code’: ‘USD’, ‘2. From_Currency Name’: ‘United States Dollar’, ‘3. To_Currency Code’: ‘INR’, ‘4. To_Currency Name’: ‘Indian Rupee’, ‘5. Exchange Rate’: ‘71.51500000’, ‘6. Last Refreshed’: ‘2018-12-13 17:40:36’, ‘7. Time Zone’: ‘UTC’}}
After parsing :
Realtime Currency Exchange Rate for United States Dollar TO Indian Rupee is 71.51500000 INR
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...