[英]want to compare two columns in excel with unique column using openpyxl
I am pretty new to python and been learning for past couple weeks now.我对 python 很陌生,并且在过去的几周里一直在学习。 I want to compare two columns in excel with unique column using openpyxl.
我想将 excel 中的两列与使用 openpyxl 的唯一列进行比较。
Excel1: Excel1:
Area_Code Representative Name
1 Jim
2 Pam
3 Mike
4 Ryan
5 Kelly
6 Andy
7 Robert
11 Roy
Excel2: Excel2:
Area_Code Representative_Name
1 Jim
2 Pam
3 Mike
4 Ryan
5 Kelly
6 Andy
7 Roberto
8 Oscar
9 Angela
10 Packer
Area code is unique.区号是唯一的。 I want to be able to list Any typo's in 'Representative_Code'.
我希望能够列出“Representative_Code”中的任何错字。 For eg: Area code 7 is Unique, but in excel2, I have a difference in name from Excel1.
例如:区号 7 是唯一的,但在 excel2 中,我与 Excel1 的名称不同。 At the same time, I want to list rows that are in excel1 and not in excel2 and vice versa.
同时,我想列出 excel1 中而不是 excel2 中的行,反之亦然。
I have written below code, but am not able to achieve my requirements.我写了下面的代码,但无法达到我的要求。 Any help would be greatly appreciated.
任何帮助将不胜感激。 Thank You!
谢谢你!
import openpyxl as xl
wb1 = xl.load_workbook('Excel1.xlsx')
sheet1 = wb1['Sheet 1']
wb2 = xl.load_workbook('Excel2.xlsx')
sheet2 = wb2['Sheet 1']
for row1 in range(2, sheet1.max_row + 1):
cell1 = sheet1.cell(row1, 2)
for row2 in range(2, sheet2.max_row + 1):
cell2 = sheet2.cell(row2, 2)
if cell1.value == cell2.value:
print(f' {cell1.value}, {cell2.value}')
Load ur data to dicts:将您的数据加载到字典:
dict1 = dict()
dict2 = dict()
for row in range(2, sheet1.max_row+1):
if sheet1['A{}'.format(row)].value:
dict1[sheet1['A{}'.format(row)].value] = sheet1['B{}'.format(row)].value
for row in range(2, sheet2.max_row+1):
if sheet2['A{}'.format(row)].value:
dict2[sheet2['A{}'.format(row)].value] = sheet2['B{}'.format(row)].value
And now u can get what u want from this two dict:现在你可以从这两个字典中得到你想要的:
diff_set_not_in_excel2 = dict1.keys() - dict2.keys()
diff_set_not_in_excel1 = dict2.keys() - dict1.keys()
intersection_set = dict1.keys() & dict2.keys()
for key in intersection_set:
if dict1[key] != dict2[key]:
print(dict1[key], dict2[key])
Of course , u also can get Representative_Name
value from the keys of differ_set.当然,你也可以从
Representative_Name
的keys中获取Conceptor_Name的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.