I am a few weeks into learning python properly and couldn't find a way to proceed from what I currently have to get each line in the input file to print an output line in the format date = total. So everything after the date is added into the total.
The desired result is:
2006-04-10 = 1399.46
2006-04-11 = 2822.36
2006-04-12 = 2803.81
2006-04-13 = 1622.71
2006-04-14 = 3119.60
2006-04-15 = 2256.14
2006-04-16 = 3120.05
2006-04-20 = 1488.00
From data60.txt which contains these values:
2006-04-10,836.2,563.263
2006-04-11,462.06,1694.3,666.0
2006-04-12,1318.19,1485.62
2006-04-13,49.714,304.0,1269.0
2006-04-14,1360.0,1731.0,28.6
2006-04-15,998.879,890.264,367.0
2006-04-16,757.4,1501.05,861.6
2006-04-20,1218.0,270.0
This is what I currently have:
def print_daily_totals(filename):
"""For each line in the input file, print an output line
in the format date = total
"""
infile = open(filename)
lines = infile.readline()
pieces = filename.split(',')
date = pieces[0]
total = float(sum(pieces[1::]))
print("{0} = {1}".format(date, total))
lines = infile.readline()
infile.close()
print_daily_totals('data60.txt')
I think I'm getting confused with splitting. Help will be greatly appreciated!