Skip to content

Instantly share code, notes, and snippets.

@CVertex
Created November 21, 2018 23:59
Show Gist options
  • Save CVertex/c66e0c64b887f092fb4ca5510b8b271f to your computer and use it in GitHub Desktop.
Save CVertex/c66e0c64b887f092fb4ca5510b8b271f to your computer and use it in GitHub Desktop.
Playing around with difflib module in python 2.7
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