比较2个文件并丢弃公共线 [英] Compare 2 files and discard common lines

查看:61
本文介绍了比较2个文件并丢弃公共线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较2个文本文件并写入第3个文件

只显示在第2个文件中但不在第1个文件中的那些行。


而不是重新发明轮子我想知道是否有人已经写了

任何东西?

解决方案

5月29日下午6:36,loial< jldunn2 ... @ googlemail.comwrote:


我要求比较2个文本文件并写入第3个文件

只显示第2个文件中出现但第1个文件中没有的行。


而不是重新发明轮子我我想知道是否有人已经写过

了吗?



您可以使用cmp(x,y)函数判断字符串是否类似于

另一个。
< cmp(''spam'',''eggs'')将返回1(垃圾邮件大于鸡蛋)

(不知道为什么)

交换两个给-1

并且''egg''和''egg''给出0.


就是你的意思正在寻找?


Kalibr写道:


5月29日下午6:36,loial< jldunn2 ... @ googlemail.comwrote:


>我要求比较2个文本文件并写入第3个文件
只有那些那些行出现在第二个文件中但不出现在第一个文件中。

我不知道是否有人已经写过
任何东西而不是重新发明轮子?



您可以使用cmp(x,y)函数来判断字符串是否类似于

另一个字符串。



或==。


Stefan


< blockquote> loial写道:


我需要比较2个文本文件并写入第3个文件

只显示那些出现在第二个文件但不在第一个文件中。



lines_in_file2 = set(open(" file2")。readlines())

for line in open(" file1") :

如果行不在lines_in_file2中:

打印行


Stefan


I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.

Rather than re-invent the wheel I am wondering if anyone has written
anything already?

解决方案

On May 29, 6:36 pm, loial <jldunn2...@googlemail.comwrote:

I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.

Rather than re-invent the wheel I am wondering if anyone has written
anything already?

You can use the cmp(x, y) function to tell if a string is similar to
another.

going cmp(''spam'', ''eggs'') will return 1 (spam is greater than eggs)
(have no idea why)
swapping the two give -1
and having ''eggs'' and ''eggs'' gives 0.

is that what you were looking for?


Kalibr wrote:

On May 29, 6:36 pm, loial <jldunn2...@googlemail.comwrote:

>I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.

Rather than re-invent the wheel I am wondering if anyone has written
anything already?


You can use the cmp(x, y) function to tell if a string is similar to
another.

Or "==".

Stefan


loial wrote:

I have a requirement to compare 2 text files and write to a 3rd file
only those lines that appear in the 2nd file but not in the 1st file.

lines_in_file2 = set(open("file2").readlines())
for line in open("file1"):
if line not in lines_in_file2:
print line

Stefan


这篇关于比较2个文件并丢弃公共线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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