Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Get the real time currency exchange rate?
Python is excellent at handling API calls for retrieving real-time and historical currency exchange rates. This article demonstrates two primary methods: using the forex-python module and making direct API calls.
Using forex-python Module
The forex-python module provides the most direct way to get currency conversion rates. It offers simple functions that accept currency codes and return conversion rates.
Real-time Exchange Rates
Here's how to get live currency conversion rates ?
from forex_python.converter import CurrencyRates
c = CurrencyRates()
# Get USD to GBP conversion rate
rate = c.get_rate('USD', 'GBP')
print(f"1 USD = {rate} GBP")
1 USD = 0.7357387755 GBP
Historical Currency Rates
You can retrieve historical exchange rates by adding a datetime object to specify the date ?
from forex_python.converter import CurrencyRates
import datetime
c = CurrencyRates()
# Get historical rate for March 27, 2020
historical_date = datetime.datetime(2020, 3, 27, 11, 21, 13, 114505)
rate = c.get_rate('USD', 'INR', historical_date)
print(f"1 USD = {rate} INR on {historical_date.date()}")
1 USD = 75.4937596793 INR on 2020-03-27
Using Web API
Many APIs provide currency rates through HTTP requests. The response is typically in JSON format, which can be easily processed in Python ?
import requests
# Replace 'your_api_key_here' with actual API key
url = 'https://v6.exchangerate-api.com/v6/your_api_key_here/latest/USD'
try:
response = requests.get(url)
data = response.json()
if data['result'] == 'success':
rates = data['conversion_rates']
print(f"USD to EUR: {rates['EUR']}")
print(f"USD to GBP: {rates['GBP']}")
print(f"USD to JPY: {rates['JPY']}")
else:
print("Error fetching data")
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
Parsing Specific Currency Rates
You can extract specific currency rates from the API response ?
import json
# Sample JSON response (shortened for demonstration)
json_data = '''
{
"result": "success",
"base_code": "USD",
"conversion_rates": {
"USD": 1,
"EUR": 0.8532,
"GBP": 0.7357,
"JPY": 109.65,
"INR": 74.12
}
}
'''
data = json.loads(json_data)
rates = data['conversion_rates']
# Display specific rates
currencies = ['EUR', 'GBP', 'JPY', 'INR']
for currency in currencies:
print(f"1 USD = {rates[currency]} {currency}")
1 USD = 0.8532 EUR 1 USD = 0.7357 GBP 1 USD = 109.65 JPY 1 USD = 74.12 INR
Comparison of Methods
| Method | Installation Required | Historical Data | API Key Required |
|---|---|---|---|
forex-python |
Yes (pip install) | Yes | No |
| Web API | No (requests only) | Depends on service | Usually yes |
Conclusion
Use forex-python for quick prototyping and simple currency conversions. For production applications requiring more control and features, consider direct API calls with proper error handling and rate limiting.
