【问题标题】:Python unicode character conversion for Emoji表情符号的 Python unicode 字符转换
【发布时间】:2017-05-27 01:35:44
【问题描述】:

我在将字节顺序标记格式化为 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

【问题讨论】:

    标签: python string unicode formatting emoji


    【解决方案1】:

    我猜decode 就是你要找的,

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

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

    的问题
    print unicode(b)
    

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

    【讨论】:

    • 搞定了!感谢@nu11p01n73R 的帮助。这种不规律的原因是什么?这会以什么方式改变输出?
    • @lindsay 我已经添加了解释。希望对你有帮助
    • @nu11p01n73R,命令后显示的emoji是什么数据类型,b的数据类型是什么?
    • @AkshayKathpal b 的类型为 str
    猜你喜欢
    • 1970-01-01
    • 2021-11-17
    • 2019-05-20
    • 2015-10-18
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2021-05-27
    相关资源
    最近更新 更多