All of a sudden str.decode('unicode_escape') stopped working [2.7.3]
Question: This snippet is taken from my recent python work. And it used to work just
fine

    
    
    strr = "What is th\u00e9 point?"
    print strr.decode('unicode_escape')
    

But now it throws the unicode decoding error:

    
    
    Traceback (most recent call last):
      File "C:\Users\Lenon\Documents\WorkDir\pyWork\ocrFinale\F1\tests.py", line 49, in <module>
        print strr.decode('unicode_escape')
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128)
    

What is the possible cause of this?


Answer: You either have enabled unicode literals or have created a _Unicode_ object by
another means, by mistake.

The `strr` value is _already_ a unicode object, so in order to decode the
value Python first tries to _encode_ to a byte string.

If you have an actual byte string your code works:

    
    
    >>> strr = "What is th\u00e9 point?"
    >>> strr.decode('unicode_escape')
    u'What is th\xe9 point?'
    

but as soon as `strr` is in fact a Unicode object, you get the error as Python
tries to _encode_ the object using the default ASCII codec first:

    
    
    >>> strr.decode('unicode_escape').decode('unicode_escape')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128)
    

It could be that you enabled `unicode_literals`, for example:

    
    
    >>> from __future__ import unicode_literals
    >>> strr = "What is th\u00e9 point?"
    >>> type(strr)
    <type 'unicode'>
    >>> strr.decode('unicode_escape')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128)
    

