now I have the string of unicode
code like "\u8fea\u514b"
,how do I convert it to the real unicode
object like u"\u8fea\u514b"
in python.
Asked
Active
Viewed 102 times
0
-
1Looking at the docs never hurts - [Unicode HOWTO](https://docs.python.org/2.7/howto/unicode.html#unicode-howto) – wwii Nov 30 '14 at 15:10
2 Answers
0
>>> s = "\u8fea\u514b"
>>> type(s)
<type 'str'>
>>> s.decode('unicode-escape')
u'\u8fea\u514b'
# OR
>>> new_s = unicode(s, 'unicode-escape')
>>> type(new_s)
<type 'unicode'>
>>> new_s
u'\u8fea\u514b'
You can type cast from string to Unicode unsing Unicode
class.
class unicode(basestring)
| unicode(string [, encoding[, errors]]) -> object
|
| Create a new Unicode object from the given encoded string.
| encoding defaults to the current default string encoding.

Tanveer Alam
- 5,185
- 4
- 22
- 43
0
Just use string.decode('unicode-escape')
>>> "\u8fea\u514b".decode('unicode-escape')
u'\u8fea\u514b'

Mazdak
- 105,000
- 18
- 159
- 188