-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparkasse.py
executable file
·92 lines (77 loc) · 2.63 KB
/
sparkasse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# coding: utf-8
"""
Run with
./sparkasse.py
"""
import io
import csv
import sys
import logging
import time
from decimal import Decimal
from converter import Converter
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Sparkasse(Converter):
"""
Implementation for Sparkasse bank
"""
def __init__(self, *args, **kwargs):
super(Sparkasse, self).__init__(*args, **kwargs)
def load_transactions(self, filename):
data = []
with io.open(filename, 'r', encoding='cp1252') as csvfile:
rd = csv.DictReader(csvfile, delimiter=';')
for row in rd:
data.append(row)
return data
def convert_row(self, line):
"""convert csv line to ynab entry"""
def process_comment(spk_comment):
if spk_comment.startswith('SVWZ+'):
spk_comment.replace('SVWZ+', '', 1)
return spk_comment
def process_amount(spk_amount):
return Decimal(spk_amount.replace(',', '.'))
def process_date(spk_date):
timestamp = time.strptime(spk_date, "%d.%m.%y")
return time.strftime('%m/%d/%y', timestamp)
if 'Umsatz getätigt von' in line:
# Credit Card
comment = line['Transaktionsbeschreibung']
amount = process_amount(line['Buchungsbetrag'])
date = process_date(line['Belegdatum'])
payee = self.find_payee(comment)
category = ""
memo = comment
elif 'Auftragskonto' in line:
# SEPA
spk_payee = line['Beguenstigter/Zahlungspflichtiger']
comment = process_comment(line['Verwendungszweck'])
amount = process_amount(line['Betrag'])
date = process_date(line['Valutadatum'])
payee = self.find_payee(spk_payee, comment)
category = ""
memo = "%s: %s" % (spk_payee, comment) if spk_payee else comment
else:
logger.error("Unrecognized CSV format")
sys.exit(1)
ynab = {
'Date': date,
'Payee': payee,
'Category': category,
'Memo': memo,
'Outflow': -amount if amount < 0 else '',
'Inflow': amount if amount > 0 else ''
}
return ynab
if __name__ == '__main__':
ynab_file = "ynab_data_sparkasse.csv"
spk_file = sys.argv[1]
converter = Sparkasse()
input_data = converter.load_transactions(spk_file)
ynab_data = []
for row in input_data:
ynab_data.append(converter.convert_row(row))
converter.export_file(ynab_file, ynab_data)