Search code examples
pythonpython-2.7python-unicodeunicode-normalization

Unicodedata works in terminal but not on .py


I am trying to remove the accents from Spanish words. If I do

import unicodedata

name="Tecnología"
uname=unicode(name, "utf-8")
nameclean=unicodedata.normalize(u"NFKD", uname).encode("ascii", erros="ignore")

In the terminal version of python it works and returns "Tecnologia", but inside the script .py it doesn't. I don't get it. The error I receive is

TypeError: decoding Unicode is not supported

specifically for the first part of the command unicodedata.normalize(u"NFKD", uname). I have made sure I am using the same sentence for both cases and I split the commands to know exactly where is the problem.

I am running python 2.7.5 and unicodedata2 12.0.0 (pip install didn't find unicodedata, also have unicode and Unicode installed)


Edit:

This is how the terminal looks like. Everything works fine, but when used inside a .py it returns the error I mentioned.

enter image description here


Solution

  • The reported exception is raised when Python is asked to decode a unicode instance to unicode.

    >>> name = u"Tecnología"
    >>> uname = unicode(name, "utf-8")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: decoding Unicode is not supported