简体   繁体   中英

how to get sum row into xls by xlwt

I have a tuple like

t = ((a,b,1,2),(a,b,3,4),(a,c,1,3),(c,d,3,6))

I used xlwt's wb to write a.xls file. But now I neeed add a sum row below like:

 C1 | C2 | C3 | C4
 a | b | 1 | 2
 a | b | 3 | 4
 a | c | 1 | 3
 c | d | 3 | 6
total: | 8 | 15

How to do this?

For this specific example, you can use this list comprehension

# assumes all rows have same number of columns

# changes the structure of table
transpose = [[row[i] for row in table] for i in range(len(table[0]))]

# store sum of transpose[-2] and transpose[-1] in total
total = [sum(transpose[i]) for i in range(-2,0)]

You will have to change the second line of code according to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM