gpt4 book ai didi

python - 在 Python 中循环 excel 工作表和行

转载 作者:太空狗 更新时间:2023-10-30 02:21:01 24 4
gpt4 key购买 nike

我有一个简单的代码:

import xlrd  
book = xlrd.open_workbook('import.xls')
for sheet in book.sheets():
for row in range(sheet.nrows):
print sheet.row(row)

但它正在打印:

sheet1: row1  
sheet1: row2
sheet1: row3

sheet2: row1
sheet2: row2
sheet2: row3
and etc

我需要更改此代码以打印此内容:

sheet1: row1  
sheet2: row1
sheet3: row1

sheet1: row2
sheet2: row2
sheet3: row2
and etc.

任何帮助将不胜感激。谢谢

最佳答案

import xlrd
book = xlrd.open_workbook('import.xls')

max_nb_row = 0
for sheet in book.sheets():
max_nb_row = max(max_nb_row, sheet.nrows)

for row in range(max_nb_row) :
for sheet in book.sheets() :
if row < sheet.nrows :
print sheet.row(row)

关于python - 在 Python 中循环 excel 工作表和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18630521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com