Search ignoring accents in Python
In this article we will learn a simple way to run a search ignoring accents in Python, a problem that happens in our language.
Hey you programmer, ok? Let’s learn more about Python!
In Python we have a very interesting and easy way to search in a string ignoring the accent
We need to use the unidecode library, and convert the words to another encode
And in doing so we run our search with ignored accent, let’s see it in practice:
import unidecode word = "programação" wordWithoutAccent = unidecode.unidecode(word) print(wordWithoutAccent) if(wordWithoutAccent.find('cao')): print("Found!")
Here we have the word programming that has an accent, and we use the unidecode method of the unidecode lib to reformat this string without this accent
Then we use find to check if we find ‘cao’ in the word programming without an accent
And so we perform the search ignoring the accent, see the output:
We can do this in lists too, check it out:
words = [ u"programação", u"acentuação", u"relação", u"será", ] def to_ascii(ls): for i in range(len(ls)): ls[i] = unidecode.unidecode(ls[i]) to_ascii(words) print(words)
Here we create a function that will remove the accent, it basically iterates through each item in the list and uses the same method seen before
The output is:
['programacao', 'acentuacao', 'relacao', 'sera']
This way we can now search this list without any problems, as the accent was removed, thanks to the function we created
In this article we earned how to run a search ignoring accents in Python
We use the unidecode library to remove the accent from the word or words in a list that we need to fetch
Then, just use the find method with the string we want to check in each phrase or word
Do you want to learn more about Python? Click here!