Python unicode character conversion for Emoji

2020-04-11 08:09发布

I'm having some issues with formatting a byte ordered mark to unicode. There is some oddness coming in with how my character is being expressed. Basically it's not printing an emoji character in Python, instead it's just the string. Here's my example.

# these codes are coming from a json file; this a representation of one of the codes.
e = 'U+1F600' # smile grin emoji

# not sure how to clean this, so here's a basic attempt using regex.
b = re.compile(r'U\+', re.DOTALL).sub('\U000', e)

print unicode(b) # output should be '\U0001F600'

For whatever reason this doesn't print an emoji character.

However if you type out the same string as a literal, using the u flag everything works as expected.

print u'\U0001F600'

What am I doing wrong here? I thought that the unicode function would convert my string to the working equivalent, but it apparently is not.

I'm using Python 2.7

1条回答
Root(大扎)
2楼-- · 2020-04-11 08:23

I guess decode is what you are looking for,

>>> b = '\U0001F600'
>>> print b.decode('unicode-escape')
                                                                    
查看更多
登录 后发表回答