4

我有一个简单的代码:

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.  

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

4

1 回答 1

7
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)
于 2013-09-05T08:03:53.597 回答