在Excel中将python代码的输出写入多行

我正在尝试在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) 

row+=1之后的下一行添加col = 0