-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
31 lines (25 loc) · 1000 Bytes
/
parser.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
from datetime import datetime
class Parser:
def __init__(self, date_field, date_format, description_field, amount_field, amount_negated):
self.date_field = date_field
self.date_format = date_format
self.description_field = description_field
self.amount_field = amount_field
self.amount_negated = amount_negated
def parse(self, row):
try:
amount = float(row[self.amount_field])
if self.amount_negated:
amount = -amount
return {
"date": datetime.strptime(row[self.date_field], self.date_format),
"description": row[self.description_field],
"amount": amount
}
except ValueError:
return {}
except IndexError:
return {}
parsers = { 'amex': Parser(0, "%m/%d/%Y %a", 2, 7, True),
'bofa': Parser(0, "%m/%d/%Y", 1, 2, False),
'chase': Parser(1, "%m/%d/%Y", 3, 4, False)}