Created
September 19, 2012 00:53
-
-
Save tylermenezes/3747001 to your computer and use it in GitHub Desktop.
Parses an ACH download from Square1Bank and turns it into a CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import sys | |
| class Transaction: | |
| def __init__(self, lines): | |
| for line in lines.split("\n"): | |
| parts = re.sub('\s+', ' ', line.strip()).split(' ') | |
| if(parts[0][0:8] == 'CUSTOMER'): | |
| if (parts[1][0:4] == '****'): | |
| self.card = parts[1][5:] | |
| else: | |
| self.card = ' '.join(parts[1:6-len(parts)]) | |
| self.date = parts[-1] | |
| elif (parts[0][0:7] == 'ACCOUNT'): | |
| self.amount = float(parts[3].replace(',', '')) | |
| if (parts[4] == 'DR'): | |
| self.amount = -1 * self.amount | |
| elif (parts[0][0:11] == 'DESCRIPTION'): | |
| self.description = parts[1] | |
| elif (parts[0][0:2] == 'ID'): | |
| if (len(parts) > 2): | |
| self.id = parts[2] | |
| else: | |
| self.id = 'UNKNOWN' | |
| if (len(self.id) > 4 and self.id[0:4] == '****'): | |
| cc = self.id[5:] | |
| self.id = self.card | |
| self.card = cc | |
| def __str__(self): | |
| return self.id + "," + self.card + "," + self.date + "," + str(self.amount) + "," + self.description | |
| f = open(sys.argv[1], 'r') | |
| transactions = [] | |
| lasttrans = '' | |
| for line in f: | |
| if (len(line) <= 1): | |
| if (len(lasttrans) > 1): | |
| transactions.append(Transaction(lasttrans)) | |
| lasttrans = '' | |
| else: | |
| lasttrans = lasttrans + "\n" + line | |
| for transaction in transactions: | |
| print str(transaction) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment