I would like to ask how do the following conversion (source->target) by Python program.
>>> source = '\\x{4e8b}\\x{696d}'
>>> print source
\x{4e8b}\x{696d}
>>> print type(source)
<type 'str'>
>>> target = u'\u4e8b\u696d'
>>> print target.encode('utf-8')
事業
Thank you.
You can use int
and unichr
to convert them:
>>> int('4e8b', 16)
20107
>>> unichr(int('4e8b', 16))
u'\u4e8b'
>>> print unichr(int('4e8b', 16))
事
Taking advantage of Blender's idea, you could use re.sub with a callable replacement argument:
import re
def touni(match):
return unichr(int(match.group(1), 16))
source = '\\x{4e8b}\\x{696d}'
print(re.sub(r'\\x\{([\da-f]+)\}', touni, source))
yields
事業
import re
p = re.compile(r'[\W\\x]+')
print ''.join([unichr(int(y, 16)) for y in p.split(source) if y != ''])
事業
also stole idea from @Blender...
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.