表情符号的Python unicode字符转换 [英] Python unicode character conversion for Emoji

查看:1026
本文介绍了表情符号的Python unicode字符转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些格式化字节顺序标记到Unicode的问题。我的角色如何被表达出来有一些奇怪的地方。基本上它不是在Python中打印表情符号,而只是字符串。这是我的例子。

 #这些代码来自json文件;这是代码之一的表示。 
e ='U + 1F600'#smile smile emoji

#不知道如何清理这个,所以这是一个使用正则表达式的基本尝试。
b = re.compile(r'U\ +',re.DOTALL).sub('\U000',e)

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

无论什么原因,这不会打印表情符号字符。 b
$ b

然而,如果您输入的字符串与字面值相同,则使用 u 标志,一切都按预期工作。

  print u'\U0001F600'

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



我使用Python 2.7

解决方案

c $ c> decode 就是你要找的,

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

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

解决方案

I guess decode is what you are looking for,

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

                        

这篇关于表情符号的Python unicode字符转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆