-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsvStatement.py
28 lines (24 loc) · 1.05 KB
/
CsvStatement.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
import csv
from datetime import date, datetime
from decimal import Decimal
def parseDate( csvDate ):
return datetime.strptime(csvDate, '%d/%m/%Y').date()
class Transaction(object):
def __init__(self, csvDict):
try:
self.date = parseDate(csvDict['Transaction Date'])
self.type = csvDict['Transaction Type']
self.description = csvDict['Transaction Description']
self.creditAmount = (Decimal(csvDict['Credit Amount'] or '0') -
Decimal(csvDict['Debit Amount' ] or '0'))
except:
print csvDict
raise
# This doesn't really work because the CSV files contain quote characters that
# aren't at the beginning of a value and DictReader doesn't spot them.
class CsvStatement(object):
def __init__(self, csvFile):
entries = csv.DictReader(csvFile)
self.transactions = [Transaction(entry) for entry in entries]
self.beginDate = max([t.date for t in self.transactions])
self.endDate = min([t.date for t in self.transactions])