lin*_*say 3 python string unicode formatting 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'
Run Code Online (Sandbox Code Playgroud)
无论出于何种原因,这都不会打印出表情符号字符.
但是,如果您输入与文字相同的字符串,使用该u标志一切都按预期工作.
print u'\U0001F600'
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我认为该unicode函数会将我的字符串转换为工作等效字符,但显然不是.
我正在使用Python 2.7
nu1*_*73R 12
我想decode你正在寻找,
>>> b = '\U0001F600'
>>> print b.decode('unicode-escape')
Run Code Online (Sandbox Code Playgroud)
要么
>>> print unicode(b, 'unicode-escape')
Run Code Online (Sandbox Code Playgroud)
这个问题
print unicode(b)
Run Code Online (Sandbox Code Playgroud)
是unicode函数尝试将字符串转换\U0001F600为unicode导致\\U0001F600.为了防止这种情况,我们提供当前编码为unicode-escape
| 归档时间: |
|
| 查看次数: |
10606 次 |
| 最近记录: |