13

I would like to compare 2 strings and have True if the strings are identical, without considering the accents.

Example : I would like the following code to print 'Bonjour'

if 'séquoia' in 'Mon sequoia est vert':
    print 'Bonjour'
Arnaud P
  • 12,022
  • 7
  • 56
  • 67
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    Convert to fully decomposed normal form, remove accents, compare. – tripleee Dec 22 '13 at 13:17
  • Linked: https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string – Basj May 22 '20 at 17:20

3 Answers3

12

You should use unidecode function from Unidecode package:

from unidecode import unidecode

if unidecode(u'séquoia') in 'Mon sequoia est vert':
    print 'Bonjour'
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Suor
  • 2,845
  • 1
  • 22
  • 28
6

You should take a look at Unidecode. With the module and this method, you can get a string without accent and then make your comparaison:

def remove_accents(data):
    return ''.join(x for x in unicodedata.normalize('NFKD', data) if x in string.ascii_letters).lower()


if remove_accents('séquoia') in 'Mon sequoia est vert':
    # Do something
    pass

Reference from stackoverflow

Community
  • 1
  • 1
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
6

(sorry, late to the party!!)

How about instead, doing this:

>>> unicodedata.normalize('NFKD', u'î ï í ī į ì').encode('ASCII', 'ignore')
'i i i i i i'

No need to loop over anything. @Maxime Lorant answer is very inefficient.

>>> import timeit
>>> code = """
import string, unicodedata
def remove_accents(data):
    return ''.join(x for x in unicodedata.normalize('NFKD', data) if x in string.ascii_letters).lower()
"""
>>> timeit.timeit("remove_accents(u'séquoia')", setup=code)
3.6028339862823486
>>> timeit.timeit("unicodedata.normalize('NFKD', u'séquoia').encode('ASCII', 'ignore')", setup='import unicodedata')
0.7447490692138672

Hint: less is better

Also, I'm sure the package unidecode @Seur suggested has other advantages, but it is still very slow compared to the native option that requires no 3rd party libraries.

>>> timeit.timeit("unicodedata.normalize('NFKD', u'séquoia').encode('ASCII', 'ignore')", setup="import unicodedata")
0.7662729263305664
>>> timeit.timeit("unidecode.unidecode(u'séquoia')", setup="import unidecode")
7.489392042160034

Hint: less is better

Putting it all together:

clean_text = unicodedata.normalize('NFKD', u'séquoia').encode('ASCII', 'ignore')
if clean_text in 'Mon sequoia est vert':
    ...
Javier Buzzi
  • 6,296
  • 36
  • 50