如果符合特定条件,如何从文本文件中查找列中的值总和

How to find the sum of values in a column from a text file if matching certain criteria

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

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(status 等于 'A' 且 finaltotal=amountpaid 的总收入总和。)

我一直收到 "TypeError: unsupported operand type(s) for +: 'int' and 'str'"。我正在使用 Python 3.4.3 和 Python 的完全新手。任何帮助将不胜感激。

因为在这一行,

total = (sum(totalamountpaid))

sum函数应用于字符串

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

sum("4322")

相当于

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

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

实际上您的代码存在很多问题。我认为您需要进行这些更改才能使其正常工作。请参阅注释(# 之后的文字)以获取解释。未测试。

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)

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

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

试试这个。

total = (sum(totalamountpaid)) 

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

拆分字符串映射中的每个数字,将字符串转换为 int。然后总结一下。

...假设第三列应该等于 '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)

已测试。 Returns24113,即6661+13220+4232.

只需要利用'totalrev'变量,在每次执行'for loop'时将'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,这正是您要找的