使用 DictReader 和 DictWriter 我需要找到 file1.csv 和 file2.csv 之间的匹配值。如果找到匹配项,请将其从 file1.csv 中删除
文件1.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
文件2.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
通过删除匹配的用户更新 file1.csv 的两个文件比较后的预期输出
UserName,LastIP,LastLogon
Tracy_Chung,10.10.10.25,11/15/2019
但是,我的代码似乎并非如此。我究竟做错了什么?
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)
牧羊人nacy
相关分类