Skip to content

Instantly share code, notes, and snippets.

@twneale
Created December 6, 2013 15:26
Show Gist options
  • Save twneale/7826458 to your computer and use it in GitHub Desktop.
Save twneale/7826458 to your computer and use it in GitHub Desktop.
How to compare two strings with difflib
>>> import difflib
>>> for opcode in difflib.SequenceMatcher(None, "This is a test.", "That is a tent").get_opcodes():
... print opcode
...
('equal', 0, 2, 0, 2)
('replace', 2, 4, 2, 4)
('equal', 4, 12, 4, 12)
('replace', 12, 13, 12, 13)
('equal', 13, 14, 13, 14)
('delete', 14, 15, 14, 14)
>>> for block in difflib.SequenceMatcher(None, "This is a test.", "That is a tent").get_matching_blocks():
... print block
...
Match(a=0, b=0, size=2)
Match(a=4, b=4, size=8)
Match(a=13, b=13, size=1)
Match(a=15, b=14, size=0)
>>>
@Med116
Copy link

Med116 commented Dec 6, 2013

i meant thanks for this script! but the previos one was cool too - mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment