Skip to content

Instantly share code, notes, and snippets.

@timwis
Last active March 17, 2016 16:04
Show Gist options
  • Save timwis/1614de5f74ad37f6f0d6 to your computer and use it in GitHub Desktop.
Save timwis/1614de5f74ad37f6f0d6 to your computer and use it in GitHub Desktop.
import sys
import csv
from collections import defaultdict
reader = csv.reader(iter(sys.stdin.readline, ''), delimiter=';')
# Skip header row
reader.next()
grouped_dict = defaultdict(lambda: ['', '', '', 0.0, 0.0, 0.0, 0.0, 0.0])
for row in reader:
# Trim whitespace in each field and remove last column (it's empty)
row = [field.strip() for field in row[:-1]]
# Strip month and day from tax period, leaving only the year
row[2] = row[2][:4]
# Group by parcel number and tax period, summing the numeric columns
group = grouped_dict[(row[0], row[2])]
for index in range(3, 8):
# If there's a minus at the end, move it to the front so it can be parsed
if row[index][-1] == '-':
row[index] = '-' + row[index][:-1]
group[index] += float(row[index])
# Convert grouped_rows dict back to list of lists
grouped_rows = [list(key) + totals for key, totals in grouped_dict.iteritems()]
for row in grouped_rows: print row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment