4

I have a simple code:

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

but it's printing:

sheet1: row1  
sheet1: row2  
sheet1: row3  

sheet2: row1  
sheet2: row2   
sheet2: row3  
and etc   

I need to change this code to print this:

sheet1: row1  
sheet2: row1  
sheet3: row1  

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

Any help would be highly appreciated. Thanks

1 Answer 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.