Skip to content

Instantly share code, notes, and snippets.

@hnykda
Last active August 26, 2021 08:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnykda/b8912ec1db311d53426520a95d35d112 to your computer and use it in GitHub Desktop.
Save hnykda/b8912ec1db311d53426520a95d35d112 to your computer and use it in GitHub Desktop.
Python snippet to convert Revolut export to Wallet
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=";")
@hnykda
Copy link
Author

hnykda commented Jun 3, 2019

@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.

@troych
Copy link

troych commented Jul 25, 2019

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.

@hnykda
Copy link
Author

hnykda commented Jul 25, 2019

Yeah, that's annoying. I guess that could be solved by changing thousands here.

@troych
Copy link

troych commented Jul 25, 2019

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment