0

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!

2 Answers2

1

First, You are not iterating over all lines of files
Other things you are converting wrong float(sum(pieces[1::])). pieces is a list of strings. You can't sum strings, so you need to convert them to floats before you sum with sum([float(i) for i in pieces[1:]])

def print_daily_totals(filename):
  """For each line in the input file, print an output line
  in the format date = total
  """    

  with open(filename, 'r') as f:
    for line in f:
      pieces = line.split(',')
      date = pieces[0]
      total = sum([float(i) for i in pieces[1:]])
      print("{0} = {1:.2f}".format(date, total))  

print_daily_totals('data60.txt') 

Output:

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  

EDIT

If you are not aware of with statement and list comprehension

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.readlines()

  for line in lines:
    pieces = line.split(',')
    date = pieces[0]
    total = 0
    for i in pieces[1:]:
      total += float(i) 
    print("{0} = {1:.2f}".format(date, total))       

  infile.close()
  
print_daily_totals('filetest.csv')
Suryaveer Singh
  • 577
  • 2
  • 13
  • 1
    Good answer. Just one nit to pick: to format the `total` to two decimal places, use `{1:.2f}` in the format string instead of `{1}` – Pranav Hosangadi Aug 25 '20 at 14:43
  • Thanks for a well explained answer! We havnt been taught 'with statements' or whatever so are you able to do it with a for loop, while loop or if statement ? – penguins1234 Aug 25 '20 at 14:51
  • also with this line "total = sum([float(i) for i in pieces[1:]])" how do you transform it into a for loop formatting such as: for value in content[1:]: total = total + float(value) – penguins1234 Aug 25 '20 at 14:53
  • `with` statement simplifies exception handling more https://stackoverflow.com/questions/3012488/what-is-the-python-with-statement-designed-for – Suryaveer Singh Aug 25 '20 at 14:58
  • `total = sum([float(i) for i in pieces[1:]])` is called list comprehension read https://stackoverflow.com/questions/20639180/explanation-of-how-nested-list-comprehension-works – Suryaveer Singh Aug 25 '20 at 14:59
  • Thank you for the edit and the links. You have helped me alot! – penguins1234 Aug 25 '20 at 15:20
  • Are you able to help me with a variation of this question aswell? Should I post a new post for that? – penguins1234 Aug 25 '20 at 15:25
0

I think it should be:

total = sum(float(i) for i in pieces[1:])
Renaud
  • 2,709
  • 2
  • 9
  • 24