简体   繁体   English

如何在python中计算Diff

[英]How calculate Diff in python

I have two strings: stringA and stringB .我有两个字符串: stringAstringB

I want to calculate difference between stringA and stringB such that it only contains the difference between both.我想计算stringAstringB之间的差异,使其仅包含两者之间的差异。 If I apply the difference on stringA , I should be able to get stringB .如果我在stringA上应用差异,我应该能够得到stringB

In python there is difflib , but it does not computer diff as above it almost saves the content of both strings.在 python 中有difflib ,但它不像上面的计算机 diff 它几乎保存了两个字符串的内容。

Example:例子:

stringA = "apple\nball\n"
stringB = "apple\ncat\n"

Now difference should look like -2,+2cat\\n , its just an example but I want difference to be minimum information.现在差异应该看起来像-2,+2cat\\n ,它只是一个例子,但我希望差异是最少的信息。

Now if I apply the above difference to stringA I should get stringB .现在,如果我将上述差异应用于stringA我应该得到stringB

You can use difflib library to do it.您可以使用difflib库来做到这一点。 Look at unified_diff function.看看unified_diff函数。

Use difflib.unified_diff from the standard library.使用标准库中的difflib.unified_diff

>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'):
...     sys.stdout.write(line)   
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
guido

If you do not want the context, just suppress it with by passing n=0 to difflib.unified_diff() :如果您不想要上下文,只需将n=0传递给difflib.unified_diff()来抑制它:

>>> import difflib
>>> a = ['line 1\n', 'line 2\n', 'line 3']
>>> b = ['line 1\n', 'line 5\n', 'line 3']
>>> delta = list(difflib.unified_diff(a, b, n=0))
>>> delta
['--- \n', '+++ \n', '@@ -2 +2 @@\n', '-line 2\n', '+line 5\n']

You may further reduce the output size by compressing it using gzip or other algorithms.您可以通过使用gzip或其他算法对其进行压缩来进一步减小输出大小。

Unfortunately, Python does not offer a way to restore unified diffs.不幸的是,Python 没有提供恢复统一差异的方法。 You may write your own code (it's not that difficult) or use one of the existing libraries from the web (an example is python-patch ).您可以编写自己的代码(这并不难)或使用网络上现有的库之一(例如python-patch )。

You can also use the patch(1) tool available on most Unix systems.您还可以使用大多数 Unix 系统上可用的patch(1)工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM