简体   繁体   中英

How calculate Diff in python

I have two strings: stringA and stringB .

I want to calculate difference between stringA and stringB such that it only contains the difference between both. If I apply the difference on stringA , I should be able to get stringB .

In python there is difflib , but it does not computer diff as above it almost saves the content of both strings.

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.

Now if I apply the above difference to stringA I should get stringB .

You can use difflib library to do it. Look at unified_diff function.

Use difflib.unified_diff from the standard library.

>>> 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() :

>>> 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.

Unfortunately, Python does not offer a way to restore unified diffs. 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 ).

You can also use the patch(1) tool available on most Unix systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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