Difflib question

Jeff Epler jepler at unpythonic.net
Tue Sep 17 14:08:44 CEST 2002


On Mon, Sep 16, 2002 at 01:08:44PM -0700, Robin Siebler wrote:
> Is there a way to get difflib to output *only* the lines that are
> different (instead of all of the lines)?

you can almost certainly do this by going "lower-level" yourself, and
using difflib.SequenceMatcher directly.  roughly:
    # create a sequence matcher object
    m = difflib.SequenceMatcher(None, s1, s2)

    # get the opcodes
    ops = m.get_opcodes()

    for op, i1, i2, j1, j2 in ops:
	# Do something with each opcode
	pass

see my pyunidiff program at http://unpythonic.net/~jepler/pyunidiff.py
for a more complex example.

Jeff




More information about the Python-list mailing list