Last active
August 26, 2021 08:05
-
-
Save hnykda/b8912ec1db311d53426520a95d35d112 to your computer and use it in GitHub Desktop.
Python snippet to convert Revolut export to Wallet
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
import argparse | |
import datetime | |
import pandas as pd | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('input', help='Input revolut export file') | |
parser.add_argument('--output', default="rev2wall-{CURRENCY}-{TIMESTAMP}.csv", help='Input Revolut file') | |
return parser.parse_args() | |
def load_df(input_file): | |
_df = (pd | |
.read_csv(input_file, sep=';', parse_dates=['Completed Date '], thousands=',', na_values=[' ']) | |
.rename(columns=lambda x: x.strip()) | |
) | |
return _df | |
def get_currency(df): | |
return df.filter(like='Paid Out').columns[0][-4:-1] | |
def wrangle_format(_df, cur): | |
df = (_df | |
.assign( | |
currency=cur, | |
paid_out_currency=lambda x: x['Paid Out ({})'.format(cur)].astype(float), | |
paid_in_currency=lambda x: x['Paid In ({})'.format(cur)].astype(float), | |
amount=lambda x: x.paid_in_currency.where(x.paid_in_currency.notna(), -x.paid_out_currency), | |
date=lambda x: pd.to_datetime(x['Completed Date']).dt.strftime('%Y/%m/%d'), | |
note=lambda x: (x['Notes'] + ' ' + x['Category']).str.strip(), | |
type=lambda x: (x['paid_in_currency'].notna()).map({True: "Income", False: "Expense"}), | |
) | |
.rename(columns=lambda x: x.lower()) | |
.filter(['date', 'currency', 'amount', 'note', 'type']) | |
) | |
return df | |
def main(input_file): | |
_df = load_df(input_file) | |
cur = get_currency(_df) | |
df = wrangle_format(_df, cur) | |
return df, cur | |
if __name__ == "__main__": | |
args = parse_args() | |
df, cur = main(args.input) | |
try: | |
output = args.output.format(CURRENCY=cur, TIMESTAMP=pd.datetime.now().strftime("%Y%m%dT%H%M%S")) | |
except KeyError: | |
output = args.output | |
df.to_csv(output, index=False, sep=";") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I've ended up doing it like this:
Works as intended now, thank you very much. :)