Reputation: 330
I have a list of strings that basically represent unicode emojis, e.g.:
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, e.g.:
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?
Upvotes: 3
Views: 745
Reputation: 27331
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)
'āšæ'
Upvotes: 3
Reputation: 2579
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')
šæ
Upvotes: 2