0

我想要最后一列的总和(即列“ E ”)并将其添加为页脚。怎么做?我没有找到任何特定的功能。虽然我找到了一个类似的帖子,但没有帮助。

def printCSVRow(argument1, argument2, argumentSwitch):
 argument1_output = None
 argument2_output = None
 prettyTable.title = 'B1 and B2 values'
 if argument1 is not None:
    argument1_output = argument1.split("\n")[1].split(";")
    if argumentSwitch == 1:
        row = ["B1"]
        row += argument1_output
        prettyTable.add_row(row)
    else:
        row = ["B2"]
        row += argument1_output
        prettyTable.add_row(row)


if argument2 is not None:
    argument2_output = argument2.split("\n")[1].split(";")
    if argumentSwitch == 1:
        row = ["B2"]
        row += argument2_output
        prettyTable.add_row(row)
    else:
        row = ["B1"]
        row += argument2_output
        prettyTable.add_row(row)




def csvHeader(B1):
 headerNames = ["type"]
 headerNames += B1.split("\n")[0].split(";")
 prettyTable.field_names = headerNames

if __name__ == '__main__':
 Handler = main()
 B1 = Handler.run()
 B2 = None
 B3 = None
 csvHeader(B1)
 iteration = 0

while True:

    B3 = Handler.run(B1)
    if B3 == B2:
        printCSVRow(B1,B2, 1)
        print(prettyTable)
    else:
        B2 = B1
        B1 = B3

输出:

+---------------------------------------------------------------------------------------------------+
|                                          B1 and B2 values                                         |
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+
| type |  id1   | start1 | end1 |  id2  | start2 | end2 |     subseqDP    |     hybridDP    |   E   |
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+
|  B1  | target |   87   |  93  | query |   24   |  30  | CAAGGGU&ACCCUUG | (((((((&))))))) | -3.62 |
|  B2  | target |   39   |  45  | query |   98   | 104  | UCCUGGA&UCCAGGA | (((((((&))))))) | -4.37 |
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+

预期输出:

+---------------------------------------------------------------------------------------------------+
|                                          B1 and B2 values                                         |
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+
| type |  id1   | start1 | end1 |  id2  | start2 | end2 |     subseqDP    |     hybridDP    |   E   |
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+
|  B1  | target |   87   |  93  | query |   24   |  30  | CAAGGGU&ACCCUUG | (((((((&))))))) | -3.62 |
|  B2  | target |   39   |  45  | query |   98   | 104  | UCCUGGA&UCCAGGA | (((((((&))))))) | -4.37 |
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+
|                                         Total                                             |  -7.99|                                     
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+
4

1 回答 1

0

如果我了解您在做什么,您想要求和的值是列表中的最后一项row,您可以访问为row[-1](负索引表示最后一个元素)但请注意,如果您想使用此值是一个字符串对其进行算术运算,您必须将字符串转换为浮点数。

这是您的函数(有点简化),其中包含计算块total末尾的if代码和输出最后一行的代码(如Shubham Shaswat此评论中所建议的)在函数的最后。

def printCSVRow(argument1, argument2, argumentSwitch):

    prettyTable.title = 'B1 and B2 values'
    total = 0

    if argument1 is not None:
        row = ["B2"] + argument1.split("\n")[1].split(";")
        if argumentSwitch == 1: row[0] = "B1"
        prettyTable.add_row(row)
        total += float(row[-1])

    if argument2 is not None:
        row = ["B1"] + argument2.split("\n")[1].split(";")
        if argumentSwitch == 1: row[0] = "B2"
        prettyTable.add_row(row)
        total += float(row[-1])

    prettyTable.add_row(["Total", "%.2f"%total])
于 2020-02-10T22:23:14.957 回答