简体   繁体   English

需要比较两个不同工作簿中的测试用例,如果它们匹配,则将同一行其他列中的结果比较到新工作簿中

[英]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. 我有两张具有相似工作表的excel工作簿,这些工作表包含测试用例,其中一个可能比另一个有更多的测试用例。 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). 我试图将匹配的测试用例的结果带到一个新文件中(最好在excel中),该结果在不同的列中可用。 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

I think it would be better if you use the set() datatype. 我认为如果使用set()数据类型会更好。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较和匹配来自同一 dataframe 不同列的数据 - How to compare and match data from different columns of same dataframe 比较不同excel工作簿中的2列:Python - Compare 2 columns in different excel workbooks: Python 如何比较两个不同的 excel 工作表中的列,如果找到匹配项,则复制其他列值 - How to compare columns in two different excel sheets and if match found copy other other column values 比较 Dataframe 两列的相同和不同 - Compare Same and Different in Two Columns of Dataframe 如何比较两个数据框并为同一行中两列相同的条目创建一个新的 - How to compare two dataframes and create a new one for those entries which are the same across two columns in the same row 在python中比较两个.xlsx工作簿并将差异打印到3.工作簿中 - Compare Two .xlsx workbooks and print differences into a 3. workbook in python 如何比较 python 中两个值是否相同但不同的情况 - how to compare if two values are the same but different cases in python 如果匹配来自同一行列的复制值,则比较并循环第二个数据帧中时间间隔的每一行 - Compare and loop every row of in between time in a second dataframe, if match copy values from columns of the same row DataFrame:比较两个不同列的日期 - DataFrame : Compare dates from two different columns 比较来自两个不同数据框熊猫的列 - Compare columns from two different dataframes pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM