我正在尝试在 Excel 工作表中编写 python 代码的输出。
这是我的尝试:
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
关于如何获得期望的结果有什么想法吗?谢谢
最佳答案
您看到这种行为是因为您没有在行末尾将 col
设置回零。
但是,您应该使用内置的 enumerate()
它为您处理增量。
for row, c in enumerate(inputdata):
for col, d in enumerate(c):
sheet.write(row,col,d)
关于python - 将Python代码的输出写入Excel中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26810892/