Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converts Sparkasse CVS file FOR CREDIT CARDS to YNAB4 CVS format.
#!/usr/bin/env python3
# Converts Sparkasse CVS file FOR CREDIT CARDS to YNAB4 CVS format.
#
# For the main account extract, see Markus Seidl's sparkasse.py at
# https://github.com/ma-ver-ick/ynab-converter/
# usage: sparkasse-credit.py umsatz-[number]-[date].CSV credit-ynab4.csv
import os
import sys
import csv
import locale
import re
BUDGETCUR = 'EUR'
LOCALE = 'de_DE.UTF-8'
locale.setlocale(locale.LC_NUMERIC, LOCALE)
if len(sys.argv) > 1:
inf = open(sys.argv[1], 'rt', encoding='iso-8859-1')
else:
inf = sys.sdtdin
if len(sys.argv) > 2:
outf = open(sys.argv[2], 'wt', encoding='utf-8')
else:
outf=sys.stdout
r = csv.DictReader(inf, delimiter=';')
w = csv.writer(outf, delimiter=',', quotechar='"')
w.writerow(['Date', 'Payee', 'Category', 'Memo', 'Outflow', 'Inflow'])
for row in r:
date = row['Belegdatum'].replace('.', '/')
payee = row['Transaktionsbeschreibung']
category = ''
memo = row['Transaktionsbeschreibung Zusatz']
origcur = row['Originalwährung']
origflow = row['Originalbetrag']
if origcur == BUDGETCUR:
flow = origflow
else:
assert(row['Buchungswährung'] == 'EUR')
exchange_rate = row['Umrechnungskurs']
flow = row['Buchungsbetrag']
memo += '(%s %s ÷ %s)' % (origflow, origcur, exchange_rate)
if locale.atof(flow) < 0:
inflow = ''
outflow = re.sub('^-', '', flow)
else:
inflow = flow
outflow = ''
w.writerow([date,payee,category,memo,outflow,inflow])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment