【问题标题】:How to find the sum of values in a column from a text file if matching certain criteria如果匹配某些条件,如何从文本文件中查找列中值的总和
【发布时间】:2016-08-08 07:29:33
【问题描述】:

我在尝试将文本文件中的列中的某些值添加在一起时遇到了一些问题。我的文本文件如下所示:

e320,2/3/5,6661,c120,A,6661
e420,6/5/3,16916,c849,A,24323
e432,6/5/3,6962,c8429,A,4324
e430,6/5/3,4322,c8491,A,4322
e32042,2/3/5,13220,c1120,A,13220
e4202,6/5/3,4232,c8419,E,4232

我想找到最后一列值的总和,前提是数组中的第三列(最终总数)等于最后一列。 (支付的金额。)。仅当第五列的(状态)等于“E”且 finaltotal == amountpaid 时,才能找到最后一列值的总和。

到目前为止,我的代码是:

data = open("paintingJobs.txt", "r")
info=data.readlines()
data.close
totalrev=0
for li in info:
    status=li.split(",")[4]
    finaltotal=int(li.split(",")[2])
    amountpaid=int(li.split(",")[5])
    if amountpaid == finaltotal:
        revenue=True
        if status == "A" and revenue == True:
            totalamountpaid = li.split(",")[5]
            total = (sum(totalamountpaid))
            print("The total revenue is")
            print(total)

我想要的输出是:

The total revenue is
28435

总计应等于 28435,因为 6661+4322+13220+4232=28435(状态等于“A”且 finaltotal=amountpaid 的总收入之和。)

我不断收到“TypeError:+ 的不支持的操作数类型:'int' 和 'str'”。我正在使用 Python 3.4.3 和一个完整的 Python 新手。任何帮助将不胜感激。

【问题讨论】:

  • 为什么要total = 28435
  • 第三列应该等于'E'还是'A'?您在问题中写了“E”,但代码中有“A”。

标签: python python-3.x text input output


【解决方案1】:

试试这个。

total = (sum(totalamountpaid)) 

total = (sum(map(int,totalamountpaid.split(',')))) 

从字符串映射中拆分每个数字,将字符串转换为 int。然后总结它们。

【讨论】:

    【解决方案2】:

    ...假设第三列应该等于'E'

    data = open("test.txt", "r")
    info=data.readlines()
    s = sum([int(li.split(',')[5]) for li in info if li.split(",")[4]=="E" and int(li.split(",")[2])==int(li.split(",")[5])])
    print("The total revenue is")
    print(s)
    

    经过测试。返回24113,即6661+13220+4232。

    【讨论】:

      【解决方案3】:

      您正在从文本文件中获取字符串。这意味着您首先需要将值转换为适当的数据类型(来自字符串),然后再将它们相加。

      尝试将此行 total = (sum(totalamountpaid)) 更改为 total = (sum(Decimal(totalamountpaid)))total = (sum(float(totalamountpaid)))

      【讨论】:

        【解决方案4】:

        只需要使用“totalrev”变量并在每次执行“for循环”时将“amountpaid”相加,并且只添加由您的条件确定的数字。最后,您只需在打印语句中调用它。小改动后,我删除了两行你不需要的代码。

        data = open("paintingJobs.txt", "r")
        info=data.readlines()
        data.close()
        
        totalrev=0
        for li in info:
            status=(li.split(",")[4])
            finaltotal=int(li.split(",")[2])
            amountpaid=int(li.split(",")[5])
            if amountpaid == finaltotal:
                totalrev += amountpaid
                revenue=True
                if status == "E" and revenue == True:
                    print("The total revenue is: " + str(totalrev))
        

        这适用于您提供的数据,我得到 28435 这就是您要查找的数据

        【讨论】:

          【解决方案5】:

          因为在这一行,

          total = (sum(totalamountpaid))
          

          sum 函数应用于字符串

          因此,使用您的示例数据,您可以有效地要求 python 执行此操作

          sum("4322")
          

          相当于

          0 + "4" + "3" + "2" + "2"
          

          当然,您不能将字符串添加到数值 0。因此会出现错误消息。

          实际上,您的代码存在许多问题。我认为您需要进行这些更改才能使其正常工作。参见 cmets(# 之后的字词)进行解释。未测试。

          data = open("paintingJobs.txt", "r")
          info=data.readlines()
          data.close()    ## Need the '()' to call the function
          totalrev=0
          for li in info:
              status=li.split(",")[4]
              finaltotal=int(li.split(",")[2])
              amountpaid=int(li.split(",")[5])
              if amountpaid == finaltotal:
                  revenue=True
                  if status == "A" and revenue == True:
                      totalamountpaid = li.split(",")[5]
                      ### Assuming you actually want to accumulate the sum in variable `totalrev`
                      totalrev += int(totalamountpaid)  ### you need to convert totalamountpaid to a numeric value, and add to the running total `totalrev` 
                      print("The total revenue is")
                      print(totalrev)
          

          【讨论】:

          • 我认为您必须重新阅读原始代码。来自 OP 的代码不会尝试对所有值求和。只有符合特定条件的行中的值
          • 我认为你必须重新缩进你的最后两行。
          • 这就是为什么我说它“未经测试”。我只想指出 OP 代码中的其他问题。
          猜你喜欢
          • 2018-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-17
          • 2013-01-13
          • 2022-11-24
          • 1970-01-01
          相关资源
          最近更新 更多