"""ach-eff-tot.py - Produce a report detailing ACH totals by effective date."""
def read_ach_file(filename):
"""Reads the ACH file specified by filename."""
achfile = open(filename, 'r')
lines = achfile.readlines()
"""Performs actual processing."""
lines = read_ach_file(sys.argv[1])
if (lines[i])[0] == '5': # batch header record
# batch effective date - reformat into MM/DD/YY format
bdt = '{}/{}/{}'.format((lines[i])[71:73], (lines[i])[73:75],
if (lines[j])[0] == '8': # batch control record
bdr = float((lines[j])[20:32]) / 100 # batch total debits
bcr = float((lines[j])[32:44]) / 100 # batch total credits
i = j # outer loop will go to next batch
totalcr[bdt] = totalcr[bdt] + bcr
totaldr[bdt] = totaldr[bdt] + bdr
print("Filename:\t{}\n-------------------------------\n".format(sys.argv[1]))
print ("Total Debits by Effective Date:")
print("{}\t{:.2f}".format(key, totaldr[key]))
print("-------------------------------")
print ("Total Credits by Effective Date:")
print("{}\t{:.2f}".format(key, totalcr[key]))