比较不同excel工作簿中的2列:Python

2024-10-01 00:24:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python中的Ppenpyxl比较不同工作簿中的2个Excel列。到目前为止,我得到的是:

#Load the workbooks
wkb1 = load_workbook(filename = os.path.join(srcdir, "wbk1.xlsx"))
wkb2 = load_workbook(filename = os.path.join(srcdir, "wbk2.xlsx"))
#Find the last row of the excel data
ws1 = wkb1.active
wkb1_LastRow = ws1.max_row
ws2 = wkb2.active
wkb2_LastRow = ws2.max_row

for xrow in range (1,(wkb1_LastRow+1)):  
    for yrow in range (1,(wkb2_LastRow+1)):
        print (ws1.cell(row=xrow, column=1).value, ws2.cell(row=yrow, column=1).value )
        if ws1.cell(row=xrow, column=1).value == ws2.cell(row=yrow, column=1).value:
            print('HIT')  

问题是if语句总是失败,即使这两列包含相同的值:

^{pr2}$

有什么想法吗?在


Tags: thevalueloadcellcolumnfilenamerowworkbook
1条回答
网友
1楼 · 发布于 2024-10-01 00:24:30

使用嵌套循环的FWIW不是这样做的方法。使用zip要简单得多。在

以下措施应该有效:

for src, target in zip(ws1['A'], ws2['A']):
   if src.value == target.value:
       print("Hit")

相关问题 更多 >