我有excel表,因为我有2列。
第一列有字符串。这个字符串需要与第二列的同一行进行比较。如果匹配则需要在同一行的第三列中写'匹配'。
如果它不匹配,我需要迭代excel中的所有行直到结束。 如果发现之间需要写'MATCH',否则我需要写'不匹配'
我正在使用openpyxl来执行此操作。
答案 0 :(得分:0)
您需要将列与if语句进行比较,然后将输出('match'/'No match')写入第三列。这可以通过以下示例代码完成:
for index1, cell_col1 in enumerate(ws.columns[0]): # iterates through column 1 with indexing
for index2, cell_col2 in enumerate(ws.columns[1]): # iterates through column 2 with indexing
if cell_col1.value == cell_col2.value: # if the values match
ws.cell(row=index1 + 1, column=3).value = 'Match'
break
elif index2 == ws.max_row - 1: # if the second loop is at the end of the table (so no match)
ws.cell(row=index1 + 1, column=3).value = 'No match'