python-3.x 如何比较2个不同的csv文件并输出差异

vm0i2vca  于 2023-01-14  发布在  Python
关注(0)|答案(1)|浏览(167)

我有2个CSV,分别是New. csv和Old. csv,它们大约有1K行和10列,结构如下:

如果新. csv中有一个longName(第一列),而旧. csv中没有,我希望将整个新. csv行附加到changes.csv中。
我开始这样做,但它并没有很好地工作:

def deltaFileMaker():
    with open('Old.csv', 'r', encoding='utf-8') as t1, open('New.csv', 'r', encoding='utf-8') as t2:
        fileone = t1.readlines()
        filetwo = t2.readlines()

    with open('changes.csv', 'w', encoding='utf-8') as outFile:
        for line in filetwo:
            if line not in fileone:
                outFile.write(line)


deltaFileMaker()

我也尝试过使用csv-diff,但是我找不到将其输出转换为csv文件的方法

    • 更新**
def deltaFileMaker():
    from csv_diff import load_csv, compare
    diff = compare(
        load_csv(open("old.csv",encoding="utf8"), key="longName"),
        load_csv(open("new.csv",encoding="utf8"), key="longName")
    )

        with open('changes.csv', 'w',encoding="utf8") as f:  
        w = csv.DictWriter(f, diff.keys())
        w.writeheader()
        w.writerow(diff)

deltaFileMaker()

执行此操作:

gcuhipw9

gcuhipw91#

您看过csv-diff吗?他们的website有一个可能合适的示例:

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文件。要将 that dict解析为行,请使用以下示例。* 注意:正确编写更改是很困难的,但这更像是一种概念验证-根据您的意愿进行修改 *

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

所以并不是对所有的更改都很好,但是对添加/删除的行非常有效。

相关问题