Search code examples
pythonexcelcomparexlrdxlwt

Need to compare test cases from two different workbooks and if they match then compare the results in the other columns of same row into new workbook


I am stuck while coding this thing. I have two excel workbooks with similar sheets.These sheet contains test cases, and one of them might have more test cases than the other. I am trying to take out the results of the matching test case, which are available in a different column, to a new file(excel preferably). Next I have to list of test cases that are not present in the other sheet.

import xlrd
import xlwt
m=-1
new=[]
#hard coding of default values
WB1= xlrd.open_workbook("WB1.xls")
WB2= xlrd.open_workbook("WB2.xls")
wb_output = xlwt.Workbook()
ws = wb_output.add_sheet('Comparison_sheet')
WB1_Sheet= WB1.sheet_by_index(1)
WB2_Sheet= WB2.sheet_by_index(1)
if WB1_Sheet.nrows > WB2_Sheet.nrows:
    Last_row=WB2_Sheet.nrows
else:
    Last_row=WB1_Sheet.nrows

if WB1_Sheet.ncols > WB2_Sheet.ncols:
    Last_Column=WB2_Sheet.ncols
else:
    Last_Column=WB1_Sheet.ncols
for x in range(24,Last_row):
    for counter in range(0,30):
        #checking matching test cases in next 20 test cases 
        y=x+counter
        if x>Last_row or y>Last_row:
            y=y-counter
            break;
        if WB2_Sheet.cell_value(x,0) == WB1_Sheet.cell_value(y,0):
            found=1;
            m+=1
            ws.write(m, 0,(str(x +1)))
            ws.write(m, 1,(str(WB1_Sheet.cell_value(x,0)) + "\n" +str(WB2_Sheet.cell_value(y,0))))
            ws.write(m, 2,(str(WB2_Sheet.cell_value(x,3))))
            ws.write(m, 3,(str(WB1_Sheet.cell_value(y,3))))
        if WB2_Sheet.cell_value(x,0) != WB1_Sheet.cell_value(y,0):
            break
first_match_offset=0
for x in range(m,Last_row):
    for counter in range(0,30):
            #checking matching test cases in next 20 test cases 
            #they are sorted such that the test case may appear in next 50 lines or they are not present
            y=x+counter
            if x>Last_row or y>Last_row:
                y=y-counter
                break;
    if WB2_Sheet.cell_value(x,0) == WB1_Sheet.cell_value(y,0):
        first_match_offset = y-m
print first_match_offset
wb_output.save("result.xls')
#its incomplete... need help to complete this thing

Solution

  • I think it would be better if you use the set() datatype. Just create a set for the observations in each workbook and then compare the sets, like:

    #create empty set
    setA = set()
    #then you loop all registers and add them to your set
    setA.add(WB1_Sheet.cell_value(y,0))
    
    #later create a setB for WB2
    
    #to get a list of the matching records in both sets:
    matchList = setA & setB
    
    #If you want to know the items of A that are not in B:
    missing = setA - setB
    
    #If you want to know all the discrepancies
    discrepancies = setA ^ setB