0

I want the sum of the last column ( ie., Column " E ") and add that as a footer. How to do it? I didn't find any specific function for the same. Though I found a similar post, which wasn't helpful.

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

output:

+---------------------------------------------------------------------------------------------------+
|                                          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 |
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+

The expected output :

+---------------------------------------------------------------------------------------------------+
|                                          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|                                     
+------+--------+--------+------+-------+--------+------+-----------------+-----------------+-------+
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kutty
  • 37
  • 1
  • 1
  • 8

1 Answers1

0

If I understand what you are doing the value that you want to sum is the last item in the list row, that you can access as row[-1] (the negative index means the last element) but beware that this value is a string, if you want to use arithmetic on it you have to convert the string to a floating point number.

Here it is your function (a little streamlined) with the code to compute the total at the end of the if blocks and the code to output the last line (as suggested by Shubham Shaswat in this comment) at the very end of the function.

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])
gboffi
  • 22,939
  • 8
  • 54
  • 85