[英]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.