I am trying to write the output of a python code in an excel sheet.
Here’s my attempt:
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('pyt')
row =0 # row counter
col=0 # col counter
inputdata = [(1,2,3,4),(2,3,4,5)]
for c in inputdata:
for d in c:
sheet.write(row,col,d)
col +=1
row +=1
wbk.save('pyt.xls')
Result obtained:
1 2 3 4
2 3 4 5
Desired result
row1: 1 2 3 4
row2: 2 3 4 5
Any ideas on how to get the desired result? thanks
You’re seeing that behaviour because you’re not setting col
back to zero at the end of the row.
Instead, though, you should use the built-in enumerate()
which handles the incrementing for you.
for row, c in enumerate(inputdata):
for col, d in enumerate(c):
sheet.write(row,col,d)
Answer:
Add col = 0
on the next line after row+=1
Tags: excel, pythonpython