python 两个文本文件之间的百分比差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
percentage difference between two text files
提问by Mohamed
I know that I can use cmp, diff, etc to compare two files, but what I am looking for is a utility that gives me percentage difference between two files.
我知道我可以使用 cmp、diff 等来比较两个文件,但我正在寻找的是一个实用程序,它可以为我提供两个文件之间的百分比差异。
if there is no such utility, any algorithm would do fine too. I have read about fuzzy programming, but I have not quite understand it.
如果没有这样的实用程序,任何算法也都可以。我读过模糊编程,但我还不太明白。
回答by Nadia Alramli
You can use difflib.SequenceMatcher ratiomethod
您可以使用 difflib.SequenceMatcher比率方法
From the documentation:
从文档:
Return a measure of the sequences' similarity as a float in the range [0, 1].
以 [0, 1] 范围内的浮点数形式返回序列相似性的度量。
For example:
例如:
from difflib import SequenceMatcher
text1 = open(file1).read()
text2 = open(file2).read()
m = SequenceMatcher(None, text1, text2)
m.ratio()
回答by brien
It looks like Linux has a utility called dwdiff that can give percentage differences by using the "-s" flag
看起来 Linux 有一个名为 dwdiff 的实用程序,它可以通过使用“-s”标志来给出百分比差异
回答by Michal Milkowski
Beyond Comparehas very nice file difference statistics export to csv. Differences at line level are reported so it's nice to compare source code files.
Beyond Compare有非常好的文件差异统计导出到 csv。报告了行级别的差异,因此比较源代码文件很好。