1 回答
TA贡献1776条经验 获得超12个赞
你看过吗csv-diff
?他们的网站有一个可能合适的示例:
from csv_diff import load_csv, compare
diff = compare(
load_csv(open("one.csv"), key="id"),
load_csv(open("two.csv"), key="id")
)
这应该返回一个dict对象,您可以将其解析为 CSV 文件。要将字典解析为行,这是一个示例。注意:正确编写更改很困难,但这更多的是概念验证 - 根据您的意愿进行修改
from csv_diff import load_csv, compare
from csv import DictWriter
# Get all the row headers across all the changes
headers = set({'change type'})
for key, vals in diff.items():
for val in vals: # Multiple of the same difference 'type'
headers = headers.union(set(val.keys()))
# Write changes to file
with open('changes.csv', 'w', encoding='utf-8') as fh:
w = DictWriter(fh, headers)
w.writeheader()
for key, changes in diff.items():
for val in changes: # Add each instance of this type of change
val.update({'change type': key}) # Add 'change type' data
w.writerow(val)
对于文件one.csv:
id, name, age
1, Cleo, 4
2, Pancakes, 2
和two.csv:
id, name, age
1, Cleo, 5
3, Bailey, 1
4, Elliot, 10
运行此命令会产生:
change type, name, id, changes, age, key
added, Bailey, 3, , 1,
added, Elliot, 4, , 10,
removed, Pancakes, 2, , 2,
changed, , , "{'age': ['4', '5']}", , 1
因此并不适合所有更改,但对于添加/删除的行非常有效。
添加回答
举报