[英]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.我用xlwt的wb写了一个.xls文件。 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.您必须根据需要更改第二行代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.