Created
December 30, 2012 01:45
-
-
Save joneskoo/4410471 to your computer and use it in GitHub Desktop.
Sort Wells Fargo CSV account activity by actual payment date, not post date
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
#!/usr/bin/env python | |
# Sort Wells Fargo CSV account activity by actual payment date, not post date | |
# By Joonas Kuorilehto. This script is Public Domain. | |
# Input: CSV file downloaded from Wells Fargo account activity | |
# Output: list of payments by transaction date | |
import re | |
import csv | |
import sys | |
from datetime import datetime, date | |
PATTERNS = [re.compile(p) for p in [ | |
r'\b(?P<month>\d\d)/(?P<day>\d\d)\b', | |
r'\bON (?P<month>\d\d)-(?P<day>\d\d)', | |
r'[^\d](?P<month>\d\d)/(?P<day>\d\d)([^\d]|$)', | |
]] | |
def parse(lines): | |
for line in lines: | |
if len(line) == 0: | |
continue # skip line | |
date_raw, amount, unknown, checkno, description = line | |
postdate = datetime.strptime(date_raw, "%m/%d/%Y") | |
# Use this year as the default year | |
month, day, year = None, None, datetime.now().year | |
for pattern in PATTERNS: | |
match = re.search(pattern, description) | |
if match: | |
if match.group("month"): | |
month = match.group("month") | |
if match.group("day"): | |
day = match.group("day") | |
if not day or not month: | |
year, month, day = 1999, 12, 31 | |
paydate = date(int(year), int(month), int(day)) | |
yield dict(postdate=postdate, amount=amount, checkno=checkno, | |
description=description, paydate=paydate) | |
def main(): | |
if len(sys.argv) != 2: | |
print "Usage: %s <file>" % sys.argv[0] | |
sys.exit(1) | |
with open(sys.argv[1]) as f: | |
payments = parse(csv.reader(f)) | |
payments = sorted(payments, key=lambda x: x["paydate"]) | |
for p in payments: | |
date = datetime.strftime(p["paydate"], "%Y-%m-%d") | |
print "%s %9s %s" % (date, p["amount"], p["description"]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment