469,287 Members | 2,752 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

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

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.

Dec 27 '05 #1
3 1585
>>>>> "Chris" == Chris Song <fa*******@gmail.com> writes:
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

--
Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
Web: http://employees.org/~rganesan | http://rganesan.blogspot.com
Dec 27 '05 #2
Ganesan Rajagopal wrote:
unicodeStrFromNetwork = '\u5927'
unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork)


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


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

</F>

Dec 27 '05 #3
>>>>> "Fredrik" == Fredrik Lundh <fr*****@pythonware.com> writes:
Ganesan Rajagopal wrote:
unicodeStrFromNetwork = '\u5927'
unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork)
How about unicodeStrNative = eval("u'%s'" % (unicodeStrFromNetwork,))

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


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

Ganesan

--
Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
Web: http://employees.org/~rganesan | http://rganesan.blogspot.com
Dec 27 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.