Python:比较两个csv文件并打印出差异 [英] Python : Compare two csv files and print out differences

查看:504
本文介绍了Python:比较两个csv文件并打印出差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个CSV文件,并在第三个CSV文件中打印出差异. 在我的情况下,第一个CSV是名为hash.csv的哈希的旧列表,第二个CSV是包含新哈希和新哈希的新哈希列表.

I need to compare two CSV files and print out differences in a third CSV file. In my case, the first CSV is a old list of hash named old.csv and the second CSV is the new list of hash which contains both old and new hash.

这是我的代码:

import csv
t1 = open('old.csv', 'r')
t2 = open('new.csv', 'r')
fileone = t1.readlines()
filetwo = t2.readlines()
t1.close()
t2.close()

outFile = open('update.csv', 'w')
x = 0
for i in fileone:
    if i != filetwo[x]:
        outFile.write(filetwo[x])
    x += 1
outFile.close()

第三个文件是旧文件的副本,而不是更新文件. 怎么了 ?我希望你能帮助我,非常感谢!

The third file is a copy of the old one and not the update. What's wrong ? I Hope you can help me, many thanks !!

PS:我不想使用diff

PS : i don't want to use diff

推荐答案

问题是您正在将fileone中的每一行与filetwo中的同一行进行比较.只要一个文件中有多余的一行,您就会发现这些行再也不相等了.试试这个:

The problem is that you are comparing each line in fileone to the same line in filetwo. As soon as there is an extra line in one file you will find that the lines are never equal again. Try this:

with open('old.csv', 'r') as t1, open('new.csv', 'r') as t2:
    fileone = t1.readlines()
    filetwo = t2.readlines()

with open('update.csv', 'w') as outFile:
    for line in filetwo:
        if line not in fileone:
            outFile.write(line)

这篇关于Python:比较两个csv文件并打印出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆