Last active
September 4, 2023 16:49
-
-
Save JCGoran/87ebd449e75526ac973ffe4a2d712c37 to your computer and use it in GitHub Desktop.
Download ECB exchange rate XML and convert it into a Python dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Method for obtaining the exchange rate from the ECB. | |
""" | |
from __future__ import annotations | |
import xml.etree.ElementTree as ET | |
import requests | |
def get_exchange_rate() -> dict[str, float]: | |
""" | |
Fetch the exchange rate and return the dictionary with the currency (in ISO 4217 format) as key and | |
the value (as a float) relative to the Euro. | |
""" | |
url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml' | |
exchange_rates = requests.get(url) | |
tree = ET.fromstring(exchange_rates.text) | |
data = [item.attrib for item in tree[-1][0]] | |
return { | |
item['currency'] : float(item['rate']) for item in data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment