文件比较的逻辑 [英] Logic for a file compare

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

问题描述

我试图写一个程序文件比较。例如:

I trying to write a programm for file compare. For example:

file1

1
2
3
4
5

file2

1
2
@
3
4
5

如果我逐行做,我得到:

If I do it line by line, I get:

1 == 1; 
2 == 2;
3 != @;
4 != 3;
5 != 4;
  != 5;

但是,事实上,文件之间的唯一区别是@。我想要这样的东西:

But, the truth is that the only difference between the files is @. I want get something like this:

1 == 1;
2 == 2;
  != @;
3 == 3;
4 == 4;
5 == 5;

这是最好的方法吗?没有使用任何外部应用程序,如diff,fc等。

Which is the best way to do it? without using any external application, such as diff, fc, etc.

推荐答案

Python有一个非常方便的库, a href =http://docs.python.org/library/difflib.html =nofollow noreferrer> difflib 。基础的SequenceMatcher类接受两个python序列,并给你(除其他外)一系列操作码,告诉你如何从第一个序列到第二个序列(即差异)。这些格式为:

Python has a very handy library for comparing sequences called difflib. The underlying SequenceMatcher class takes two python sequences and gives you (among other things) a sequence of opcodes telling you how you would get from the first sequence to the second (i.e. the differences). These are of the form:


  • 将此块替换为

  • 插入块

  • 删除块

  • 复制块(称为等于)

这些引用块通过给原始序列赋予索引。这可以应用于文件中的行或字符串中的字符或任何其他可以转换为python中的序列。

These reference blocks by giving indices into the original sequences. This can be applied to lines in a file or characters in a string or anything else you can turn into a sequence in python.

这篇关于文件比较的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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