我需要从一个文本文件中读取输入并创建一个包含输出的新文本文件。目前我的代码读得很好,但它只写最后一行数据,没有遍历所有行。有人能帮我修好这个吗?在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()
输入是
^{pr2}$
我得到的输出是2006-04-10 = 1488.00
但正确的应该是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