difflib.Differ¶
-
class
difflib.Differ(linejunk=None, charjunk=None)[source]¶ Differ is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines.
Each line of a Differ delta begins with a two-letter code:
‘- ‘ line unique to sequence 1 ‘+ ‘ line unique to sequence 2 ‘ ‘ line common to both sequences ‘? ‘ line not present in either input sequenceLines beginning with ‘? ‘ attempt to guide the eye to intraline differences, and were not present in either input sequence. These lines can be confusing if the sequences contain tab characters.
Note that Differ makes no claim to produce a minimal diff. To the contrary, minimal diffs are often counter-intuitive, because they synch up anywhere possible, sometimes accidental matches 100 pages apart. Restricting synch points to contiguous matches preserves some notion of locality, at the occasional cost of producing a longer diff.
Example: Comparing two texts.
First we set up the texts, sequences of individual single-line strings ending with newlines (such sequences can also be obtained from the readlines() method of file-like objects):
>>> text1 = ''' 1. Beautiful is better than ugly. ... 2. Explicit is better than implicit. ... 3. Simple is better than complex. ... 4. Complex is better than complicated. ... '''.splitlines(1) >>> len(text1) 4 >>> text1[0][-1] '\n' >>> text2 = ''' 1. Beautiful is better than ugly. ... 3. Simple is better than complex. ... 4. Complicated is better than complex. ... 5. Flat is better than nested. ... '''.splitlines(1)
Next we instantiate a Differ object:
>>> d = Differ()
Note that when instantiating a Differ object we may pass functions to filter out line and character ‘junk’. See Differ.__init__ for details.
Finally, we compare the two:
>>> result = list(d.compare(text1, text2))
‘result’ is a list of strings, so let’s pretty-print it:
>>> from pprint import pprint as _pprint >>> _pprint(result) [' 1. Beautiful is better than ugly.\n', '- 2. Explicit is better than implicit.\n', '- 3. Simple is better than complex.\n', '+ 3. Simple is better than complex.\n', '? ++\n', '- 4. Complex is better than complicated.\n', '? ^ ---- ^\n', '+ 4. Complicated is better than complex.\n', '? ++++ ^ ^\n', '+ 5. Flat is better than nested.\n']
As a single multi-line string it looks like this:
>>> print ''.join(result), 1. Beautiful is better than ugly. - 2. Explicit is better than implicit. - 3. Simple is better than complex. + 3. Simple is better than complex. ? ++ - 4. Complex is better than complicated. ? ^ ---- ^ + 4. Complicated is better than complex. ? ++++ ^ ^ + 5. Flat is better than nested.
Methods:
- __init__(linejunk=None, charjunk=None)
- Construct a text differencer, with optional filters.
- compare(a, b)
- Compare two sequences of lines; generate the resulting delta.
Methods¶
__init__([linejunk, charjunk]) |
Construct a text differencer, with optional filters. |
_dump(tag, x, lo, hi) |
Generate comparison results for a same-tagged range. |
_fancy_helper(a, alo, ahi, b, blo, bhi) |
|
_fancy_replace(a, alo, ahi, b, blo, bhi) |
When replacing one block of lines with another, search the blocks for similar lines; the best-matching pair (if any) is used as a synch point, and intraline difference marking is done on the similar pair. |
_plain_replace(a, alo, ahi, b, blo, bhi) |
|
_qformat(aline, bline, atags, btags) |
Format ”?” output and deal with leading tabs. |
compare(a, b) |
Compare two sequences of lines; generate the resulting delta. |