-3

I am trying to compare two sheets from my excel and list the row that is not matching from sheet1 using xlrd in python.

Sheet1:

Name    Gender  Age
John    M   30
Moses   F   28

Sheet2:

Name    Gender  Age
John    M   30
Moses   F   29

import xlrd as xl
loc = "C:/Users/S22JK3/Desktop/Sample.xlsx"
wb = xl.open_workbook(loc)
sheet1 = wb.sheet_by_index(0)
sheet2 = wb.sheet_by_index(1)

row1 = sheet1.nrows
column1 = sheet1.ncols
row2 = sheet2.nrows
column2 = sheet2.ncols

row = max(row1,row2)
column = max(column1,column2)

for i in range(row):
     for j in range(column):
        if sheet1.cell(i,j) != sheet2.cell(i,j):
print(sheet1.cell(i,j))

When i try to run the above code, I am getting error as "Expected an indented block"

Nihal
  • 5,262
  • 7
  • 23
  • 41
  • give 3 tabs at line `print(sheet1.cell(i,j))` ` – Nihal Jan 08 '19 at 10:57
  • 1
    Not tabs, you should be using 4 spaces per level of indentation. The last line needs to be indented under the `if` condition. Also, the error has nothing to do with the title, this is standard Python syntax – roganjosh Jan 08 '19 at 10:59

1 Answers1

0

Fix your code indentation and logic.

Use this:

import xlrd as xl

loc = "C:/Users/S22JK3/Desktop/Sample.xlsx"
wb = xl.open_workbook(loc)
sheet1 = wb.sheet_by_index(0)
sheet2 = wb.sheet_by_index(1)

row1 = sheet1.nrows
column1 = sheet1.ncols
row2 = sheet2.nrows
column2 = sheet2.ncols

row = max(row1,row2)
column = max(column1,column2)

for i in range(row):
    for j in range(column):
        if sheet1.cell(i,j) != sheet2.cell(i,j):
            print(sheet1.cell(i,j))
Employee
  • 3,109
  • 5
  • 31
  • 50