简体   繁体   中英

How to extract a unicode string from a string

I've a string of the form "text: u'\ل'" , how to extract in python the inner unicode string? (ie to have u'\ل' )

When I use split() I got "u'\\\ل'" which is a simple string!

You can use ast.literal_eval() to safely convert the literal string:

>>> from ast import literal_eval

>>> s = "text: u'\u0644'"

>>> unicode_part = s.split(':')[-1].strip()
>>> unicode_part
"u'\\u0644'"

>>> unicode_string = literal_eval(unicode_part)
>>> unicode_string
u'\u0644'
>>> print unicode_string
ل

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM