我正在比较两个 Excel 工作簿之间的数据,并在第三个工作簿中记录命中结果。
这是 Azure-Provider-description.xlsx:
这是 Azure-Scrape.xlsx:
这是 Azure 操作:
这是代码:
from openpyxl import load_workbook
wb1 = load_workbook("Azure-Provider-description.xlsx")
ws1 = wb1["Sheet1"]
provider = ""
description = ""
wb2 = load_workbook("Azure-Scrape.xlsx")
ws2 = wb2["Sheet1"]
for row in ws1.iter_rows(min_row=1, max_col=3):
if row[1].value == None:
pass
else:
provider = row[0].value
description = row[1].value
for cell in ws2.iter_rows(min_row=1, max_col=3):
if cell[0].value.startswith(provider) and cell[1].value == description:
cell[2].value = str("'" + cell[0].value + "',")
else:
pass
wb2.save("Azure-Actions.xlsx")
它记录了新工作簿的 c 列中的点击,但我还需要记录 Azure-Provider-description.xlsx 中与 Azure-Scrape.xlsx 中任何内容都不匹配的行 - 我需要做什么?
提前致谢。
假设您想有条件地 left-join
2nd
表与 1st
表同时保留后者的不匹配项,您可以尝试以下操作:
from openpyxl import load_workbook, Workbook
wb1 = load_workbook("Azure-Provider-description.xlsx")
ws1 = wb1["Sheet1"]
wb2 = load_workbook("Azure-Scrape.xlsx")
ws2 = wb2["Sheet1"]
merges = []
for pro1, desc1 in ws1.values:
for pro2, desc2 in ws2.values:
if pro2.startswith(pro1) and desc1 == desc2:
merges.append([pro2, desc1, f"'{desc2},"])
else:
merges.append([pro1, desc1]) # << maybe to be adjusted ?
wb3 = Workbook()
ws3 = wb3.active; ws3.title = "Sheet1"
for row in merges:
ws3.append(row)
wb3.save("Azure-Actions.xlsx")