-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqfx.py
63 lines (50 loc) · 1.32 KB
/
qfx.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
import re
def parseTransactionsFromFile(path):
"""
Parse the transactions out of a QFX file.
File format:
...
...
VERSION:102
...
...
<STMTTRN>
<TRNTYPE>DEBIT
<DTPOSTED>20120710120000[0:GMT]
<TRNAMT>-142.00
<FITID>201207100
<NAME>THE HAY MERCHANT HOUSTON
<MEMO>07/09THE HAY M
</STMTTRN>
...
...
"""
stmttrn = None
ret = []
for line in open(path, "r"):
line = line.strip()
if line.startswith("VERSION:"):
version = int(line[8:])
if version != 102:
raise Exception("invalid file version %d" % version)
elif stmttrn is not None:
if line == "</STMTTRN>":
ret.append(stmttrn)
stmttrn = None
else:
gidx = line.index(">")
stmttrn[line[1:gidx]] = re.sub("&", "&", line[gidx + 1:])
elif line == "<STMTTRN>":
stmttrn = {}
else:
# not in a statement transaction, ignore the line
pass
return ret
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
transactions = parseTransactionsFromFile(sys.argv[1])
for t in transactions:
print t
print ""
print "%d transactions" % len(transactions)