0

I need to read input from a text file and create a new text file with the output in it. At the moment my code is reading fine but it is writing only the last line of data, not going through all the lines. Can someone please help me fix this?

def generate_daily_totals(input_filename, output_filename):
  """Returns date followed by the sum of values"""
  infile = open(input_filename)

for line in infile:
    content = line.split(",")
    date = content[0]
    total = 0
    for value in content[1:]:
        total = total + float(value)
    rounded_total = "{:.2f}".format(total)
    summary = date + " " + "=" + " "  + rounded_total
  outfile = open(output_filename, "w")
  outfile.write(summary)    

generate_daily_totals('data60.txt', 'totals60.txt')
checker = open('totals60.txt')
print(checker.read())
checker.close() 

The input is

2006-04-10,836.2,563.263
2006-04-10,462.06,1694.3,666.0
2006-04-10,1318.19,1485.62
2006-04-10,49.714,304.0,1269.0
2006-04-10,1360.0,1731.0,28.6
2006-04-10,998.879,890.264,367.0
2006-04-10,757.4,1501.05,861.6
2006-04-10,1218.0,270.0

What I am getting for output is

2006-04-10 = 1488.00

But the correct one should be

2006-04-10 = 1399.46
2006-04-10 = 2822.36
2006-04-10 = 2803.81
2006-04-10 = 1622.71
2006-04-10 = 3119.60
2006-04-10 = 2256.14
2006-04-10 = 3120.05
2006-04-10 = 1488.00
2
  • 1
    I think your indentation is wrong. Surely the for loop should be inside the function? Commented Apr 14, 2018 at 9:17
  • @Dan yes you're right, it is in the function, i wrote it wrong here Commented Apr 15, 2018 at 10:13

3 Answers 3

3

You code is opening your output file afresh inside the loop. Each time it does that, it overwrites what it did the previous time through the loop.

outfile = open(output_filename, "w")
outfile.write(summary)  

Open your output file before you start looping through the input.

with open(output_filename, "w") as outfile:
  for line in infile:
    content = line.split(",")
    date = content[0]
    total = 0
    for value in content[1:]:
        total = total + float(value)
    rounded_total = "{:.2f}".format(total)
    summary = date + " " + "=" + " "  + rounded_total + "\n"
    outfile.write(summary)   
Sign up to request clarification or add additional context in comments.

7 Comments

Yes this would be more efficient than my answer.
Thank you! But how do I get it on a new line each time?
Unlike print, the write function expects you to manage your own newline characters if you want them, because if you were writing a binary file, you wouldn't want them: summary = date + " " + "=" + " " + rounded_total + '\n'
@Dan My concern was not efficiency. If you use append, then you also have to supply code to empty the target file beforehand. Otherwise the solution only works as intended the first time.
@BoarGules thank you for the help, I got it shortly after asking! :)
|
0

The problem with your code is the following line:

outfile = open(output_filename, "w")

By opening the file as a write file, you override everything else in the file. Instead you should use append:

outfile = open(output_filename, "a")

Comments

0

You need to open the output file in append mode.

def generate_daily_totals(input_filename, output_filename):
    """Returns date followed by the sum of values"""
    infile = open(input_filename)

    for line in infile:        
        content = line.split(",")
        date = content[0]
        total = sum(map(float, content[1:])) # No need for the inner loop
        rounded_total = "{:.2f}".format(total)
        summary = date + " " + "=" + " "  + rounded_total        
        outfile = open(output_filename, "a")
        outfile.write(summary + '\n')

generate_daily_totals('data60.txt', 'totals60.txt')

Output written to the file:

2006-04-10 = 1399.46
2006-04-10 = 2822.36
2006-04-10 = 2803.81
2006-04-10 = 1622.71
2006-04-10 = 3119.60
2006-04-10 = 2256.14
2006-04-10 = 3120.05
2006-04-10 = 1488.00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.