If you take a look at unified_diff code you will find description about a parameter called n:
Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three.
In your case, n basically indicates numbers of characters. If you assign a value to n, then you will get the correct output. This code:
from difflib import unified_diff
s1 = ['a', 'b', 'c', 'd', 'e', 'f']
s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n']
for line in unified_diff(s1, s2,n=6):
print line
Will generate:
---
+++
@@ -1,6 +1,12 @@
a
b
c
d
e
f
+g
+i
+k
+l
+m
+n
Answer from ahajib on Stack OverflowWhy does unified_diff method from the difflib library in Python leave out some characters? - Stack Overflow
Generating and applying diffs in python - Stack Overflow
Unified diff between a list of strings and a list of regexes?
diff - How to apply the output of python's difflib.unified_diff to the original string? - Stack Overflow
Did you have a look at diff-match-patch from google? Apparantly google Docs uses this set of algoritms. It includes not only a diff module, but also a patch module, so you can generate the newest file from older files and diffs.
A python version is included.
http://code.google.com/p/google-diff-match-patch/
Does difflib.unified_diff do want you want? There is an example here.
The original link is broken. There is an example here
I know that I can use difflib in python3 to get unified diffs between two lists of strings. However, I am looking for a way to get unified diffs between a list of strings and a list of regexes. For example, suppose I have the following items in my python3 program ...
stringlist = [
'aaaaaaaaa',
'bbbbbbbbb',
'ccccccccc',
'ddddddddd',
]
regexlist = [
re.compile(r'^(aaa)+$'),
re.compile(r'^(bbb)+$'),
re.compile(r'^(xxx)+$'),
re.compile(r'^(ccc)+$'),
re.compile(r'^(eee)+$'),
]I'd like to run this through a function similar to difflib.unified_diff() which returns a representation of the diffs between these lists in a format similar to a standard unified diff. However, the comparison between list elements must be done via re.search() instead of simply ==.
I was hoping that I could somehow supply difflib.unified_diff() with some sort of comparison function that would use re.search() ... and also a special printing function which would print the regex in a human-readable format in the unified diffs. However, I cannot find any way to do either of those things.
Does anyone know how to replace difflib's comparison function with a custom comparator and to supply it a custom printing function? Or alternatively, does anyone know of any other python3 package that can generate unified diffs using such a custom comparator and custom printing function?
Thank you in advance for any suggestions.