Python 表情符号的 unicode 字符转换

Python unicode character conversion for Emoji

我在将字节有序标记格式化为 unicode 时遇到了一些问题。我的角色的表达方式有些奇怪。基本上它不会在 Python 中打印表情符号字符,而只是字符串。这是我的例子。

# 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'

无论出于何种原因,这都不会打印表情符号字符。

但是,如果您输入与文字相同的字符串,使用 u 标志,一切都会按预期进行。

print u'\U0001F600'

我在这里做错了什么?我认为 unicode 函数会将我的字符串转换为有效的等效字符串,但显然不是。

我正在使用 Python 2.7

我想 decode 就是你要找的,

>>> b = '\U0001F600'
>>> print b.decode('unicode-escape')

>>> print unicode(b, 'unicode-escape')


的问题
print unicode(b)

unicode 函数试图将字符串 \U0001F600 转换为 unicode,从而导致 \U0001F600。为防止这种情况,我们将当前编码提供为 unicode-escape