如果匹配特定条件,如何在文本文件的列中查找值的总和

时间:2016-04-16 08:55:41

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

我在尝试将文本文件中的某些值添加到一起时遇到了一些麻烦。我的文本文件如下:

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' A'以及最终总数= amountpaid的总收入之和。)

我一直收到" TypeError:不支持的操作数类型+:' int'和' str'"。我使用Python 3.4.3和Python的完全新手。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

试试这个。

total = (sum(totalamountpaid)) 

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

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

答案 1 :(得分:1)

您正在从文本文件中提取字符串。这意味着您首先需要在添加值之前将值转换为适当的数据类型(来自字符串)。

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

答案 2 :(得分:1)

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

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

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,这就是您所寻找的

答案 4 :(得分:0)

这是因为在这一行,

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)