簡體   English   中英

將python代碼的輸出寫入excel中的多行

[英]Writing the output of a python code to multiple rows in excel

我試圖在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

暫無
暫無

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

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