比较两个CSV文件,并将差异作为python中的额外列写入同一文件

rdrgkggo  于 2023-01-06  发布在  Python
关注(0)|答案(1)|浏览(148)

嘿智能社区,
我需要一点帮助,因为我想我看不到树林里的树林。
我必须CSV文件,看起来像这样:

Name,Number
AAC;2.2.3
AAF;2.4.4
ZCX;3.5.2

Name,Number
AAC;2.2.3
AAF;2.4.4
ZCX;3.5.5

我想比较这两个文件,并比写任何更改这样:

Name,Number,Changes
AAC;2.2.3
AAF;2.4.4
ZCX;5.5.5;change: 3.5.2

所以在每一行当数字有差异时,我想在行尾添加一个新列。
文件的格式是相同的,但有时有一个新的行,所以这就是为什么我认为我必须Map的键。
我走了这么远,但现在我迷失在我的想法:
Python 3.10.9语言

import csv

Reading the first csv and set mapping
with open('test1.csv', 'r') as csvfile:
reader= csv.reader(csvfile)
rows = list(reader)
file1_dict = {row[1]: row[0] for row in rows}

Reading the second csv and set mapping
with open('test2.csv', 'r') as csvfile:
reader= csv.reader(csvfile)
rows = list(reader)
file2_dict = {row[1]: row[0] for row in rows}

comparing the keys and find the diff

for k in test1_dict:
if test1_dict[k] != test2:dict[k]
test1_dict[k] = test2_dict[k]
for row in rows:
if row[1] == k:
row.append(test2_dict[k])

#write the csv (not sure how to add the word "change:")
with open('test1.csv', 'w', newline ='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(rows)

如果我尝试这样做,我没有得到一个新的列,它只是"更新"的csv文件与相同的列。
例如,这段代码给了我一个diff行,但是我不能把它添加到现有的文件和行中。

with open('test1.csv') as fin1:
  with open('test2.csv') as fin2:
    read1 = csv.reader(fin1)
    read2 = csv.reader(fin2)
    diff_rows = (row1 for row1, row2 in zip(read1, read2) if row1 != row2)
    with open('test3.csv', 'w') as fout:
      writer = csv.writer(fout)
      writer.writerows(diff_rows)

有人能帮我解决这个问题吗?我在这里看了很多答案,但还是想不出来。
非常感谢。
@bigkeefer谢谢你的回答,我试图改变它的分隔符;但是它给出了"列表索引超出范围错误"。

with open('test3.csv', 'r') as file1:
    reader = csv.reader(file1, delimiter=';')
    rows = list(reader)[1:]
    file1_dict = {row[0]: row[1] for row in rows}
with open('test4.csv', 'r') as file2:
    reader = csv.reader(file2, delimiter=';')
    rows = list(reader)[1:]
    file2_dict = {row[0]: row[1] for row in rows}
new_file = ["Name;Number;Changes\n"]
with open('output.csv', 'w') as nf:
    for key, value in file1_dict.items():
        if value != file2_dict[key]:
            new_file.append(f"{key};{file2_dict[key]};change: {value}\n")
        else:
            new_file.append(f"{key};{value}\n")
    nf.writelines(new_file)
wz3gfoph

wz3gfoph1#

你将需要修改它来覆盖你的第一个文件等等,就像你上面提到的,但是为了你的测试目的,我把它保持这样,希望这能在某种程度上帮助你。
我假设你已经在每个文件中得到了上面的头文件,如果没有,删除创建列表时的分片,并将new_file变量赋值改为空列表([])。

with open('f1.csv', 'r') as file1:
    reader = csv.reader(file1, delimiter=";")
    rows = list(reader)[1:]
    file1_dict = {row[0]: row[1] for row in rows if row}
with open('f2.csv', 'r') as file2:
    reader = csv.reader(file2, delimiter=";")
    rows = list(reader)[1:]
    file2_dict = {row[0]: row[1] for row in rows if row}
new_file = ["Name,Number,Changes\n"]
for key, value in file1_dict.items():
    if value != file2_dict[key]:
        new_file.append(f"{key};{file2_dict[key]};change: {value}\n")
    else:
        new_file.append(f"{key};{value}\n")
with open('new.csv', 'w') as nf:
    nf.writelines(new_file)

相关问题