how to convert string like '\u5927' to unicode string u'\u5927'

C

Chris Song

Here's my solution

_unicodeRe = re.compile("(\\\u[\da-f]{4})")
def unisub(mo):
return unichr(int(mo.group(1)[2:],16))

unicodeStrFromNetwork = '\u5927'
unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork)

But I think there must be a more straightforward way to do it.
 
G

Ganesan Rajagopal

Chris" == Chris Song said:
Here's my solution
_unicodeRe = re.compile("(\\\u[\da-f]{4})")
def unisub(mo):
return unichr(int(mo.group(1)[2:],16))
unicodeStrFromNetwork = '\u5927'
unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork)

How about unicodeStrNative = eval("u'%s'" % (unicodeStrFromNetwork,))

Ganesan
 
F

Fredrik Lundh

Ganesan said:
How about unicodeStrNative = eval("u'%s'" % (unicodeStrFromNetwork,))

unicodeStrFromNetwork = "' + str(__import__('os').system('really bad idea')) + '"

</F>
 
G

Ganesan Rajagopal

unicodeStrFromNetwork = "' + str(__import__('os').system('really bad idea')) + '"

Thanks for the warning. I should really know better! *blush*

Ganesan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top