-
-
Save hnykda/b8912ec1db311d53426520a95d35d112 to your computer and use it in GitHub Desktop.
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=";") |
@troych Because you use Python 3.4, you need at least 3.6 where an order of keywords arguments is guaranteed (technically in 3.7, but 3.6+ will work as well). Python 3.4 is retired, you should get rid of it.
That worked indeed, finally had some time to update my python install.
Second problem now, for my currency (CHF) revolut uses "," to delimit. So I've got payments of 22,70 for example. This does not seem to work with your snippet, 22,70 gets converted to 2270. If I change the delimiter to "." things work correctly.
Yeah, that's annoying. I guess that could be solved by changing thousands
here.
Yeah, that's annoying. I guess that could be solved by changing
thousands
here.
Thanks, I've ended up doing it like this:
.read_csv(input_file, sep=';', parse_dates=['Completed Date '], decimal=',', na_values=[' '])
Works as intended now, thank you very much. :)
Getting this on my debian machine, any idea how to solve this?