22

I have a string like \uXXXX (representation) and I need to convert it into unicode. I receive it from 3rd party service so python interpreter doesn't convert it and I need conversion in my code. How do I do it in Python?

>>> s
u'\\u0e4f\\u032f\\u0361\\u0e4f'
John Machin
  • 78,552
  • 11
  • 135
  • 182
Soid
  • 2,310
  • 1
  • 28
  • 40

3 Answers3

24
>>> u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
u'\u0e4f\u032f\u0361\u0e4f'
>>> print u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
๏̯͡๏
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
  • 1
    `AttributeError: 'str' object has no attribute 'decode'` – Kid_Learning_C May 14 '19 at 21:58
  • 1
    @Kid_Learning_C You are probably on Python 3 then; this is an old answer which applies to Python 2. See e.g. https://stackoverflow.com/questions/48908131/decodeunicode-escape-in-python-3-a-string – tripleee Jan 29 '20 at 18:11
5

There's an interesting list of encodings supported by .encode() and .decode() methods. Those magic ones in the second table include the unicode_escape.

Tuttle
  • 150
  • 1
  • 7
3

Python3:

bytes("\\u0e4f\\u032f\\u0361\\u0e4f", "ascii").decode("unicode-escape")
Ilya Kharlamov
  • 3,421
  • 30
  • 31