比较 2 个不同的 csv 文件并将所有更改输出到新的 csv 中

我有 2 个 CSV,分别是 New.csv 和 Old.csv,如下所示:


旧的.csv


longName,shortName,eventType,number,severity

ACTAGENT201,ACAT201,RES,1,INFO

ACTAGENT202,ACAT202,RES,2,ALERT

ACODE801,AC801,ADMIN,1,MINOR

ACODE802,AC802,ADMIN,2,MINOR

ACODE102,AC102,COMM,2,CRITICAL

ACODE103,AC103,COMM,3,CRITICAL

ACODE104,AC104,COMM,4,CRITICAL

ACODE105,AC105,COMM,5,CRITICAL

ACODE106,AC106,COMM,6,CRITICAL

新.csv


longName,shortName,eventType,number,severity

ACTAGENT201,ACAT201,RES,1,INFO

ACTAGENT202,ACAT202,RES,2,ALERT

ACODE801,AC801,ADMIN,1,MINOR

ACODE802,AC802,ThisHasBeenChanged,2,MINOR

ACODE102,AC102,COMM,2,CRITICAL

ACODE103,AC103,COMM,3,CRITICAL

ACODE104,AC104,COMM,4,THISHASBEENCHANGED

ACODE105,AC105,COMM,5,CRITICAL

ACODE106,AC106,COMM,6,CRITICAL

如果行中的某一列中的数据已在 old.csv 和 new.csv 之间进行了修改/更改,则应将整行附加到changes.csv,就像 old.csv 中的每一列一样new.csv 彼此相邻:

https://img1.sycdn.imooc.com/658e68490001b5ab17620087.jpg

牛魔王的故事
浏览 97回答 1
1回答

一只萌萌小番薯

首先,将两个 CSV 文件读入字典,使用longName值作为键。import csvwith open(old_csv_file, "r") as fh:    reader = csv.reader(fh)    old_csv = {row[0]: row for row in reader}with open(new_csv_file, "r") as fh:    reader = csv.reader(fh)    new_csv = {row[0]: row for row in reader}然后,使用集合操作可以轻松找到新添加和删除的键。old_longNames = set(old_csv.keys())new_longNames = set(new_csv.keys())# common: set intersectioncommon_longNames = old_longNames.intersection(new_longNames)# removed: whatever's in old but not in newremoved_longNames = old_longNames - new_longNames# added: whatever's in new but not in oldadded_longNames = new_longNames - old_longNames最后,迭代公共集以查找有变化的地方:changed_longNames = []for key in common_longNames:    old_row = old_csv[key]    new_row = new_csv[key]    # if any(o != n for o, n in zip(old_row, new_row)):    if old_row != new_row:        # this row has at least one column changed. Do whatever        print(f"LongName {key} has changes")        changed_longNames.append(key)或者,作为列表理解:changed_longNames = [key for key in common_longNames if old_csv[key] != new_csv[key]]将所有内容写入新的 csv 文件也相当简单。请注意,这些集合不保留顺序,因此您可能无法以相同的顺序获得结果。with open("deleted.csv", "w") as fh:    writer = csv.writer(fh)    for key in removed_longNames:        writer.writerow(old_csv[key])with open("inserted.csv", "w") as fh:    writer = csv.writer(fh)    for key in added_longNames:        writer.writerow(new_csv[key])with open("changed.csv", "w") as fh:    writer = csv.writer(fh)    for key in changed_longNames:        old_row = old_csv[key]        new_row = new_csv[key]        merged_row = []        for oi, ni in zip(old_row, new_row):            merged_row.append(oi)            merged_row.append(ni)        writer.writerow(merged_row)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python