Skip to content

Commit

Permalink
interpret csv dialect, find transaction lines dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
hamvocke committed Jul 1, 2016
1 parent 10fe9df commit ecaa13f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
31 changes: 18 additions & 13 deletions dkb2homebank.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class dkb(csv.Dialect):

def convertDkbCash(filename):
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile, dialect='dkb', fieldnames=dkbFieldNames)
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.DictReader(transactionLines(csvfile), dialect=dialect, fieldnames=dkbFieldNames)

with open("cashHomebank.csv", 'w') as csvfile:
skipHeader(reader)

writer = csv.DictWriter(csvfile, dialect='dkb', fieldnames=homebankFieldNames)
with open("cashHomebank.csv", 'w') as outfile:
writer = csv.DictWriter(outfile, dialect='dkb', fieldnames=homebankFieldNames)
for row in reader:
writer.writerow(
{
Expand All @@ -67,11 +67,12 @@ def convertDkbCash(filename):

def convertVisa(filename):
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile, dialect='dkb', fieldnames=visaFieldNames)
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.DictReader(transactionLines(csvfile), dialect=dialect, fieldnames=visaFieldNames)

with open("visaHomebank.csv", 'w') as csvfile:
skipHeader(reader)
writer = csv.DictWriter(csvfile, dialect='dkb', fieldnames=homebankFieldNames)
with open("visaHomebank.csv", 'w') as outfile:
writer = csv.DictWriter(outfile, dialect='dkb', fieldnames=homebankFieldNames)
for row in reader:
writer.writerow(
{
Expand All @@ -85,9 +86,13 @@ def convertVisa(filename):
'tags': None
})

def skipHeader(reader):
for i in range(1, 6):
next(reader)
def transactionLines(file):
lines = file.readlines()
i = 1
for line in lines:
if "Betrag" in line:
return lines[i:]
i = i + 1

def convertDate(dateString):
date = datetime.strptime(dateString, "%d.%m.%Y")
Expand All @@ -114,4 +119,4 @@ def main():


if __name__ == '__main__':
main()
main()
5 changes: 5 additions & 0 deletions dkb2homebankTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def testShouldConvertVisaFile(self):
lineNumber = sum(1 for line in open('visaHomebank.csv'))
self.assertEqual(lineNumber, 5)

def testShouldConvertVisaFileWithRange(self):
dkb2homebank.convertVisa('testfiles/visaRange.csv')
lineNumber = sum(1 for line in open('visaHomebank.csv'))
self.assertEqual(lineNumber, 4)

def tearDown(self):
self.delete('cashHomebank.csv')
self.delete('visaHomebank.csv')
Expand Down
2 changes: 0 additions & 2 deletions testfiles/cash.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@
"30.03.2015";"30.03.2015";"KARTENZAHLUNG/-ABRECHNUNG";"ACME CORPORATION TEXAS";"EC 12345678 123456789012ABC";"12345678";"98765432";"-50,22";"";"";"";
"30.03.2015";"30.03.2015";"GUTSCHRIFT";"Sallack, Tom";"MUSTACHE RENT";"DE99988877766655544400";"SOMEBAN2FO";"1337,00";"";"Tom Sallack 11020";"";
"03.04.2015";"04.04.2015";"GUTSCHRIFT";"Norris, Chuck";"BEARD PAYMENT";"DE88866633321234329200";"FOOBARBAZ0";"660,23";"";"";"Some Reference";


5 changes: 2 additions & 3 deletions testfiles/visa.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"Saldo:";"31337.33 EUR";
"Datum:";"01.04.2015";

"Umsatz abgerechnet";"Wertstellung";"Belegdatum";"Umsatzbeschreibung";"Betrag (EUR)";"Ursprünglicher Betrag";
"Umsatz abgerechnet";"Wertstellung";"Belegdatum";"Beschreibung";"Betrag (EUR)";"Ursprünglicher Betrag";
"Nein";"02.04.2015";"01.04.2015";"APRIL FOOL'S PAYMENT";"-50,00";"";
"Nein";"31.03.2015";"30.03.2015";"ANOTHER PAYMENT";"-50,00";"";
"Nein";"27.03.2015";"26.03.2015";"BANANENDEPOT STOCKHOLN";"-30,00";"";
"Nein";"27.03.2015";"26.03.2015";"BANANENDEPOT STOCKHOLM";"-30,00";"";
"Ja";"23.03.2015";"21.03.2015";"POSTBANK KOPENHAGEN";"-35,00";"";
"Ja";"23.03.2015";"21.03.2015";"RABOBANK AMSTERDAM";"-20,00";"";

12 changes: 12 additions & 0 deletions testfiles/visaRange.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Kreditkarte:,1234********1234 Kreditkarte,,,,
,,,,,
Von:,02.07.2015,,,,
Bis:,30.06.2016,,,,
Saldo:,12345.67 EUR,,,,
Datum:,29.06.2016,,,,
,,,,,
Umsatz abgerechnet,Wertstellung,Belegdatum,Beschreibung,Betrag (EUR),Ursprünglicher Betrag
Nein,29.06.2016,29.06.2016,buchung1,-1234,
Nein,24.06.2016,23.06.2016,buchung2,"11,79",
Ja,07.12.2015,04.12.2015,buchung in fremdwaehrung,"-41,82","-30,00 GBP"
Nein,24.11.2015,24.11.2015,buchung2,"11,79",

0 comments on commit ecaa13f

Please sign in to comment.