I tried writing a simple python script using the python module ecbxrate that outputs to a CSV file a table with the daily exchange rate between EUR and USD since 2013.

Below is my code, it works, gives the correct output. I tried using methods rather than a long script. I specifically wanted to use Ecbxrate.

import ecbxrate 
import datetime
import pandas as pd
from sqlite3 import Error
ecb_path = 'sqlite:///D://test/db/ecb.sqlite3' #path where ecb historical database is stored
outfile = 'Downloads/Exchnage_rate.csv' # CSV table name and path 
store = ecbxrate.ExchangeRateStore(ecb_path) 
""" We can initialise it one time also from command line but i kept it as a part of script in case there is any issue it can re initialise as per given path"""
"""This initialise the ecb data if does not exist else update if it does, we can sechdule the script to be run after 3pm to get lastest exchange rate of the day 
and then it shall first update with lastest exchange rate data before writing it to file.Also I tried printing exceptions, we can follow the uniform error handling method as per standards/requirement """
def get_data():

    except Error as e:
        print("Error with ecb database", e)
        except Error as e:
            print('Error while initialising',e)
            print('unknown error , check initialise process')
        if store.last_updated() != datetime.datetime.today().date(): #if latest data doesn't exist
            except Error as e:
                print('Error while updating', e)
                print('unknown error while updating')
            print('latest data already') """ 
        """#Else part is not needed because  all excpetions already handleded, we can do it in case need to log the process for success."""

""" This gets the data from updated ECB database from given date range between given currencies"""
def get_exchange_rate(start_date, end_Date, from_curr, to_cuur):
    daterange = pd.date_range(start_date, end_Date)
    data = [store.get_rate(from_curr, to_cuur,single_date.strftime("%Y-%m-%d")) for single_date in daterange] #calling for all days in between
    #return (pd.DataFrame(data))
    d = pd.DataFrame(data) 
    d.insert(1, 'currency_from', from_curr)
    d.insert(2,'currency_to', to_cuur)
    d.rename(columns={d.columns[0]: "Date", d.columns[3]: "Exchange_Rate" },inplace=True)
    d.to_csv(outfile, index=False) # putting it in csv of given path and name

def main():
    get_exchange_rate('2013-1-1', datetime.datetime.today().date(),'EUR', 'USD')


Could you please review and let me know if it can be written better. Few doubts I have are:

  1. Should I pass ecb_path as parameter to get_data() and get_exchange_Rate()? I am not sure about the best approach.
  2. Are nested try except blocks Pythonic in this case? Can I handle it better?
  3. Should I write a different method to write output to csv file?
  4. Is for loop efficient in this case, to get data for bigger date range?
  5. Should I add a check in case the output file is not created or given path is wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.