Removing accents with Python - Unicode doesn't work
Question: I'm trying to clean a spanish text with the following code:

    
    
    import re
    import unicodedata
    file = open("dirty.txt").readlines()
    archivo = open("cleanText.txt", "w")
    textLowerCase = file[i].lower()
    unicodeText = textLowerCase.decode('unicode-escape')
    textWithoutAccents = unicodedata.normalize('NFKD', unicodeText).encode('ASCII', 'ignore')
    textWithoutSymbols = re.sub(r'[^\w]', ' ', textWithoutAccents)
    archivo.write(textWithoutSymbols)
    

My input text is like:

    
    
    "anunciarán la realización de una interpelación"
    

My desire output is:

    
    
    "anunciaran la realizacion de una interpelacion"
    

But with my code i'm getting the next result:

    
    
    "anunciarAn la realizaciA3n de una interpelaciA3n"
    

Do you know how can I solve this?


Answer: This is exactly what **unidecode** package does:
<https://pypi.python.org/pypi/Unidecode>

From the readme:

> The module exports a function that takes an Unicode object (Python 2.x) or
> string (Python 3.x) and returns a string (that can be encoded to ASCII bytes
> in Python 3.x):
    
    
    >>> from unidecode import unidecode
    >>> unidecode(u'ko\u017eu\u0161\u010dek')
    'kozuscek'
    >>> unidecode(u'30 \U0001d5c4\U0001d5c6/\U0001d5c1')
    '30 km/h'
    >>> unidecode(u"\u5317\u4EB0")
    'Bei Jing '
    

EDIT:

Your exact example:

    
    
    >>> from unidecode import unidecode
    >>> unidecode("anunciarán la realización de una interpelación")
    'anunciaran la realizacion de una interpelacion'
    

EDIT 2:

If you are using Python 2, don't forget to either:

  * `from __future__ import unicode_literals`, or:
  * use `u` prefix before string quotes: `unidecode(u"anu...`

