I have a list of strings that basically represent unicode emojis, eg:
emoji[0] = 'U+270DU+1F3FF'
I would like to convert this "almost" unicode emoji representation to its true emoji representation so that I can search through text documents that contain these emojis, eg:
emoji[0] = emoji[0].replace('U+', '\U000')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-4: truncated \UXXXXXXXX escape
How can I accomplish that?
A solution that would work with variable digit representations:
>>> import re
>>> e = 'U+270DU+1F3FF'
>>> def emojize(match):
... return chr(int(match.group(0)[2:], 16))
>>> re.sub(r"U\+[0-9A-F]+", emojize, e)
'✍🏿'
This is because you have 4 digits in 270D
and 5 in 1F3FF
:
>>> e = 'U+270D'
>>> print e.replace('U+', '\U0000').decode('unicode-escape')
✍
>>> e = 'U+1F3FF'
>>> print e.replace('U+', '\U000').decode('unicode-escape')
🏿
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.