简体   繁体   English

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

[英]Compare two CSV files and write difference in the same file as an extra column in python

Hey intelligent community,嘿智慧社区,

I need a little bit of help because i think i don't see the the wood in the trees.我需要一点帮助,因为我想我没有看到树木中的木头。

i have to CSV files that look like this:我必须要 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

I would like to compare both files and than write any changes like this:我想比较这两个文件,而不是像这样写任何更改:

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

So on every line when there is a difference in the number, i want to add this as a new column at the end of the line.所以在每一行当数字有差异时,我想将其添加为行尾的新列。
The Files are formated the same but sometimes have a new row so thats why i think i have to map the keys.文件的格式相同,但有时会有一个新行,所以这就是为什么我认为我必须使用 map 键。

I come this far but now iam lost in my thoughts:我走了这么远,但现在我迷失在我的思绪中:

Python 3.10.9 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)

If i try this, i don't get a new column, it just "updates" the csv file with the same columns.如果我尝试这样做,我不会得到一个新列,它只是用相同的列“更新”csv 文件。

For example this code gives me the diff row but i'am not able to just add it to existing file and row.例如,此代码为我提供了差异行,但我无法将其添加到现有文件和行中。

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)

Does someone have any tips or help for my problem?有人对我的问题有任何提示或帮助吗? I read many answers on here but can't figure it out.我在这里阅读了很多答案,但无法弄清楚。

Thanks alot.非常感谢。

@bigkeefer Thanks for your answer, i tried to change it for the delimiter; @bigkeefer 感谢您的回答,我尝试将其更改为分隔符; but it gives an "list index out of range error".但它给出了“列表索引超出范围错误”。

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)

You will need to adapt this to overwrite your first file etcetera, as you mentioned above, but I've left it like this for your testing purposes.正如您上面提到的,您将需要修改它以覆盖您的第一个文件等,但为了您的测试目的,我将它保留为这样。 Hopefully this will help you in some way.希望这会以某种方式帮助你。

I've assumed you've actually got the headers above in each file.我假设您实际上在每个文件中都有上面的标题。 If not, remove the slicing on the list creations, and change the new_file variable assignment to an empty list ([]).如果不是,请删除列表创建的切片,并将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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个csv文件中的列并将其写入python中的另一个文件 - Compare columns in two csv files and write it to another file in python 比较两个 csv 文件并将匹配的条目写入第三个文件 python - Compare two csv files and write the matching entries in third file python 比较2个单独的csv文件并将差异写入新的csv文件-Python 2.7 - Compare 2 seperate csv files and write difference to a new csv file - Python 2.7 比较两个csv文件并将共享项写入新的csv文件 - Compare two csv files and write shared items to a new csv file Python:如何在新的CSV文件中写入两个CSV文件(其中一个具有列子集)的差异 - Python: How to write the difference of two CSV files, with one having a subset of columns, in a new CSV file 如何按列比较两个CSV文件并使用Pandas Python将CSV文件中的差异保存 - How to compare two CSV files by column and save the differences in csv file using pandas python Python:比较CSV文件并保存与第一行的区别(列名) - Python : Compare CSV files and save the difference with first row(Column Names) 比较2个csv文件之间的列,并使用Python编写差异 - Compare a column between 2 csv files and write differences using Python Python比较两个csv文件并将数据附加到csv文件 - Python compare two csv files and append data to csv file Python Pandas:比较两个 CSV 文件并通过匹配列从两个文件中删除行 - Python Pandas: Compare two CSV files and delete lines from both the file by matching a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM