Created
December 6, 2013 15:26
-
-
Save twneale/7826458 to your computer and use it in GitHub Desktop.
How to compare two strings with difflib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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) | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i meant thanks for this script! but the previos one was cool too - mark