Sometimes, we want to do fuzzy string comparison with Python.
In this article, we’ll look at how to do fuzzy string comparison with Python.
How to do fuzzy string comparison with Python?
To do fuzzy string comparison with Python, we can use difflib
.
For instance, we write
import difflib
matches = difflib.get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
to call difflib.get_close_matches
with the search string and an array of possible matches.
get_close_matches
returns a list of close matches from the strings in the list in the 2nd argument.
We can also call get_close_matches
with keyword.kwlist
by writing
import difflib
import keyword
matches = get_close_matches('wheel', keyword.kwlist)
keyword.kwlist
is the list of keywords to search in to get close matches.
Concluson
To do fuzzy string comparison with Python, we can use difflib
.