23

I receive a string like this from a third-party service:

>>> s
'\\u0e4f\\u032f\\u0361\\u0e4f'

I know that this string actually contains sequences of a single backslash, lowercase u etc. How can I convert the string such that the '\\u0e4f' is replaced by '\u0e4f' (i.e. '๏'), etc.? The result for this example input should be '๏̯͡๏'.

0

3 Answers 3

24

In 2.x:

>>> u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
u'\u0e4f\u032f\u0361\u0e4f'
>>> print u'\\u0e4f\\u032f\\u0361\\u0e4f'.decode('unicode-escape')
๏̯͡๏
2
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.

4

Python3:

bytes("\\u0e4f\\u032f\\u0361\\u0e4f", "ascii").decode("unicode-escape")

Not the answer you're looking for? Browse other questions tagged or ask your own question.