【发布时间】: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