What is the best way to remove accents (normalize) in a Python unicode string?

后端 未结 8 1558
感情败类
感情败类 2020-11-21 06:11

I have a Unicode string in Python, and I would like to remove all the accents (diacritics).

I found on the web an elegant way to do this (in Java):

  1. conve
8条回答
  •  臣服心动
    2020-11-21 06:46

    Unidecode is the correct answer for this. It transliterates any unicode string into the closest possible representation in ascii text.

    Example:

    accented_string = u'Málaga'
    # accented_string is of type 'unicode'
    import unidecode
    unaccented_string = unidecode.unidecode(accented_string)
    # unaccented_string contains 'Malaga'and is of type 'str'
    

提交回复
热议问题