From 8a6c9ad0313ce56b9d74e76a5d1bedc6f4ed186c Mon Sep 17 00:00:00 2001 From: Remigiusz Marcinkiewicz Date: Mon, 16 Jan 2017 18:56:33 +0100 Subject: [PATCH] Strip spaces from currency amount values before parsing, reverse CSV line order for reading. --- fetch/banking-ib.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/fetch/banking-ib.py b/fetch/banking-ib.py index 79a52f1..367e335 100644 --- a/fetch/banking-ib.py +++ b/fetch/banking-ib.py @@ -96,14 +96,14 @@ class IBRow(RawTransfer): self.from_name = row[IBField.from_name] self.title = row[IBField.title] - af = re.compile(r"([0-9]+)\.([0-9]{2}) ([A-Z]+)") - m = af.match(row[IBField.amount]) + af = re.compile(r"([0-9]+)\.([0-9]{2})([A-Z]+)") + m = af.match(''.join(row[IBField.amount].split())) if m is None: raise IBParseError("Can't parse amount value \"{}\"".format(row[IBField.amount]), row) a,b,c = m.groups() self.amount = int(a)*100+int(b) self.currency = c - m = af.match(row[IBField.balance]) + m = af.match(''.join(row[IBField.balance].split())) if m is None: raise IBParseError("Can't parse balance value \"{}\"".format(row[IBField.balance]), row) a,b,c = m.groups() @@ -144,10 +144,8 @@ class IBParser(object): self.fields = [] def parse(self, snapshot): - c = csv.reader(snapshot.splitlines(), delimiter=";") - header = [r.decode("utf-8") for r in next(c, None)] - if header is None: - raise IBParseError("No header in history for {}".format(self.account_number)) + lines = snapshot.splitlines() + header = lines.pop(0).decode("utf-8").split(";") for hf in header: try: @@ -155,6 +153,7 @@ class IBParser(object): except ValueError as e: raise IBParseError("Unexpected field name \"{}\"".format(hf),e) + c = csv.reader(reversed(lines), delimiter=";") for row in c: if not len(row) == len(self.fields): raise IBParseError("Row has {} fields, {} expected after parsing the header: \"{}\"".format(len(row), len(self.fields), ';'.join(row)))