Using DictReader and DictWriter I need to find matching values between file1.csv and file2.csv. If a match is found, remove it from file1.csv
file1.csv
UserName,LastIP,LastLogon
Jessica_Alba,10.10.10.11,11/14/2019
Karen_Edwards,10.10.10.12,11/14/2019
Tracy_Chung,10.10.10.25,11/15/2019
file2.csv
Department,UserName,LastPasswordReset,LastIP
IT,Jessica_Alba,9/14/2019,10.10.10.11
Accounting,Karen_Edwards,9/14/2019,10.10.10.12
Expected output after comparison of two files that file1.csv is updated by removing the matching users
UserName,LastIP,LastLogon
Tracy_Chung,10.10.10.25,11/15/2019
However, it doesn't seem to the case with my code. What am I doing wrong?
data3 = []
with open("file1.csv","r") as in_file1, open("file2.csv", "r") as in_file2:
reader1 = csv.DictReader(in_file1)
reader2 = csv.DictReader(in_file2)
for row2 in reader2:
for row1 in reader1:
print(row1['UserName'])
if row2['UserName'] != row1['UserName']:
data3.append(row1)
print(data3)
reader2your code is iterating over all rows ofreader1androw2is appended todata3every time, except for the one time the usernames are equal.