Created
August 22, 2017 20:44
-
-
Save eseiver/39cdda697119f3cabf7cce181666066c to your computer and use it in GitHub Desktop.
Check the difference between two strings
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
# With thanks to https://stackoverflow.com/questions/17904097/python-difference-between-two-strings | |
# Describes difflib-detected differences in easy-to-understand English | |
import difflib | |
cases = [('apple', 'apples'), ('banaana', 'bananas')] | |
for a, b in cases: | |
print('{} => {}'.format(a, b)) | |
for i, s in enumerate(difflib.ndiff(a, b)): | |
if s[0] == ' ': continue | |
elif s[0] == '-': | |
print(u'Delete "{}" from position {}'.format(s[-1],i)) | |
elif s[0] == '+': | |
print(u'Add "{}" to position {}'.format(s[-1],i)) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment