Created
April 25, 2016 00:21
-
-
Save kylelk/ebd5b12126f2c68b933d215254bfe68d to your computer and use it in GitHub Desktop.
python difflib example
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 | |
# output | |
# the(insert " fast and") quick brown (replace "fox" with "duck") jumped over the lazy (replace "dog" with "cat") | |
s1 = 'the quick brown fox jumped over the lazy dog' | |
s2 = 'the fast and quick brown duck jumped over the lazy cat' | |
def show_diff(seqm): | |
output = [] | |
for opcode, a0, a1, b0, b1 in seqm.get_opcodes(): | |
if opcode == 'equal': | |
output.append(seqm.a[a0:a1]) | |
elif opcode == 'insert': | |
output.append('(insert "{}")'.format(seqm.b[b0:b1], a0)) | |
elif opcode == 'delete': | |
output.append('(delete "{}")'.format(seqm.a[a0:a1])) | |
elif opcode == 'replace': | |
output.append('(replace "{}" with "{}")'.format(seqm.a[a0:a1], seqm.b[b0:b1])) | |
# print('range {}..{} of a with {}..{} of b'.format(a0, a1, b0, b1)) | |
else: | |
raise RuntimeError("unexpected opcode") | |
return ''.join(output) | |
sm = difflib.SequenceMatcher(None, s1, s2) | |
print(show_diff(sm)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment