Last active
March 1, 2016 19:00
-
-
Save janpipek/5b30463e294259560ecb to your computer and use it in GitHub Desktop.
ČNB (Czech National Bank) exchange rates with python and pandas.
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
def read_cnb_year(year): | |
"""Read the whole history of Czech National Bank rates in one year. | |
:param year: 1991-2016 (or "today's year") | |
""" | |
import urllib.request | |
from io import StringIO | |
import pandas as pd | |
url = "https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/rok.txt?rok={0}".format(year) | |
response = urllib.request.urlopen(url) | |
data = response.read() | |
text = data.decode('utf-8') | |
# A year may be divided into different parts with different headers | |
parts = ["Datum" + part for part in text.split("Datum") if part] | |
df = pd.DataFrame() | |
for part in parts: | |
df = df.append(pd.read_csv(StringIO(part), decimal=",", delimiter="|", index_col=0, parse_dates=True, dayfirst=True)) | |
return df | |
def read_cnb(): | |
"""Read the whole history of Czech National Bank rates (1991-2016)""" | |
import pandas as pd | |
df = pd.DataFrame() | |
for yr in range(1991, 2017): | |
df = df.append(read_cnb_year(yr)) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment