-
Notifications
You must be signed in to change notification settings - Fork 0
/
amex4ynab.py
executable file
·75 lines (55 loc) · 2.05 KB
/
amex4ynab.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
#!/usr/bin/python
import fileinput
class Transaction(object):
"""docstring for Transaction"""
def __init__(self, lineString):
super(Transaction, self).__init__()
items = self.makeItems(lineString)
self.date = self.parseDate(items[0])
self.payee = items[2]
self.payer = items[3]
self.card = items[4]
self.amount = items[7]
def makeItems(self, lineStr):
return lineStr.split(',')
def parseDate(self, dateStr):
if dateStr.find("Mon") >= 0:
self.date = dateStr.replace(" Mon", "")
elif dateStr.find("Tue") >= 0:
self.date = dateStr.replace(" Tue", "")
elif dateStr.find("Wed") >= 0:
self.date = dateStr.replace(" Wed", "")
elif dateStr.find("Thu") >= 0:
self.date = dateStr.replace(" Thu", "")
elif dateStr.find("Fri") >= 0:
self.date = dateStr.replace(" Fri", "")
elif dateStr.find("Sat") >= 0:
self.date = dateStr.replace(" Sat", "")
elif dateStr.find("Sun") >= 0:
self.date = dateStr.replace(" Sun", "")
else:
print "NOT FOUND"
return self.date
def printAsLine(self):
print self.asString()
def asString(self):
return "%s,%s,%s,%s,%s" % (self.date, self.payee, self.payer, self.card, self.amount)
if __name__ == "__main__":
def headers():
return "%s,%s,%s,%s,%s" % ("Date", "Payee", "Payer", "Card", "Outflow")
def toFile(name, transactions):
splitName = name.split('.')
outFileName = splitName[0] + ".formatted." + splitName[1]
target = open(outFileName, 'w')
target.write(headers() + "\n")
for trans in transactions:
target.write(trans.asString() + "\n")
target.close()
transactions = []
for line in fileinput.input():
transactions.append(Transaction(line))
print headers()
for trans in transactions:
trans.printAsLine()
toFile(fileinput.filename(), transactions)
fileinput.close()