Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

python - 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?

question from:https://stackoverflow.com/questions/65930491/how-to-get-sum-row-into-xls-by-xlwt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...