簡體   English   中英

如何通過 xlwt 將總和行轉換為 xls

[英]how to get sum row into xls by xlwt

我有一個像

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

我用xlwt的wb寫了一個.xls文件。 但現在我需要在下面添加一個總和行,例如:

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

這個怎么做?

對於此特定示例,您可以使用此列表推導

# 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)]

您必須根據需要更改第二行代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM