Created
November 21, 2018 23:59
-
-
Save CVertex/c66e0c64b887f092fb4ca5510b8b271f to your computer and use it in GitHub Desktop.
Playing around with difflib module in python 2.7
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 | |
a = """ | |
one | |
two | |
three | |
four | |
""" | |
b = """ | |
one | |
two | |
three | |
""" | |
def _unidiff_output(expected, actual): | |
""" | |
Helper function. Returns a string containing the unified diff of two multiline strings. | |
""" | |
import difflib | |
expected=expected.splitlines(1) | |
actual=actual.splitlines(1) | |
diff=difflib.unified_diff(expected, actual) | |
return ''.join(diff) | |
diff = difflib.ndiff(a, b) | |
for d in diff: | |
#print d | |
pass | |
#print "==========" | |
delta = ''.join(x[2:] for x in diff if x.startswith('- ')) | |
#print delta | |
#print "==========" | |
difference = _unidiff_output(a,b) | |
if difference == '': | |
print "Strings are the same" | |
else: | |
print "Strings are different" | |
print difference | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment