Skip to content

Instantly share code, notes, and snippets.

@moniquelive
Created February 24, 2013 17:23
Show Gist options
  • Save moniquelive/5024687 to your computer and use it in GitHub Desktop.
Save moniquelive/5024687 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
enddays = [0, 31,28,31,30,31,30,31,31,30,31,30,31]
def output_transactions(saldo, days):
this_year = 2010 #datetime.datetime.now().year
dtstart = datetime.date(this_year, days.keys()[1].month, 1)
dtend = datetime.date(this_year, dtstart.month, enddays[dtstart.month])
for day in daterange(dtstart, dtend):
if day in days.keys():
for trans in days[day]:
action = trans['action'].split('\t')[0]
value = trans['value']
print ("%s\t%s\t%.02f" % (day, action, value)).replace('.', ',')
def daterange(start_date, end_date):
for n in range((end_date - start_date).days+1):
yield start_date + datetime.timedelta(n)
def money(x):
x = x.split()
x[0] = x[0].replace('.', '')
x[0] = x[0].replace(',', '.')
return +float(x[0]) if x[1] == 'C' else -float(x[0])
def todate(x):
if not type(x) is type(''):
return x
day = int(x.split('/')[0])
month = int(x.split('/')[1])
year = 2010
return datetime.date(year, month, day)
import sys, re, datetime, collections
lines = [x.strip() for x in sys.stdin.readlines()]
blck_start = lines.index('Movimentacao em contas')
blck_end = lines.index('', blck_start)
curday = ''
days = collections.defaultdict(list)
saldo = 0
for l in lines[blck_start:blck_end]:
m = re.search('^(\\d{2}/\\d{2})', l)
if m:
curday = m.groups()[0]
action = l[6:41]
value = l[42:]
elif not l.startswith('Saldo Total'):
action = l[0:35]
value = l[36:]
else:
continue
# once per history
if action.startswith('Saldo anterior'):
saldo = money(action.split('\t')[-1])
continue
if not curday:
continue
value = money(value)
curday = todate(curday)
days[curday] += [{"action":action, "value":value}]
output_transactions(saldo, days)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment