gpxlcj
gpxlcj

Reputation: 377

In python, how to transform the string slash to a escape sequence

I would like to know how to transform a string containing a Unicode ASCII descriptor into an actual Unicode character.

For example, how can I transform u"\\u1234" to u"\u1234" ?

Thank you.

Upvotes: 1

Views: 346

Answers (2)

georg
georg

Reputation: 215029

decode('unicode-escape'), for example:

>>> s = u'\\u03be'
>>> s
u'\\u03be'
>>> print s
\u03be
>>> e = s.decode('unicode-escape')
>>> e
u'\u03be'
>>> print e
ξ

This works, but it would be better to fix the root of the problem. Where does the string come from in the first place? Is it JSON? Some Python code?

Upvotes: 2

Lærne
Lærne

Reputation: 3142

Here is how I would do it, using ast.literal_eval. ast.literal_eval parses python literals without executing anycode embedded in it, so it's much safer to use in program.

The key idea is to evaluate a string representation of what you wish, for instance u"\1234"

import ast

s=u"\\u04d2"
s_bis=ast.literal_eval( u'u"'+s+u'"' )

edit: georg's solution is much better.

Upvotes: 0

Related Questions