Loop excel sheets and lines in Python

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 he prints:

 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 greatly appreciated. Thanks

+4
source share
1 answer
 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) 
+6
source

Source: https://habr.com/ru/post/1500668/


All Articles