代码之家  ›  专栏  ›  技术社区  ›  user9644895

用python将输出写入文件

  •  0
  • user9644895  · 技术社区  · 6 年前

    我需要从文本文件中读取输入并创建一个新的文本文件,其中包含输出。目前,我的代码读起来很好,但它只写最后一行数据,而不是所有的行。有人能帮我修一下吗?

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

    输入为

    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
    

    我从输出中得到的是

    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
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   BoarGules    6 年前

    代码正在循环中重新打开输出文件。每次这样做时,它都会覆盖上一次通过循环所做的操作。

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

    在开始循环输入之前打开输出文件。

    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)   
    
        2
  •  0
  •   Dan    6 年前

    您的代码存在以下问题:

    outfile = open(output_filename, "w")
    

    通过以写文件的形式打开文件,可以覆盖文件中的所有其他内容。相反,您应该使用append:

    outfile = open(output_filename, "a")
    
        3
  •  0
  •   srikavineehari    6 年前

    您需要在中打开输出文件 append 模式

    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')
    

    写入文件的输出:

    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