将python表情符号打印为unicode字符串

You*_*Tan 17 unicode python-2.7 emoji

我一直在努力输出 作为'\ U0001f604'而不是笑脸,但它似乎不起作用.

我尝试使用repr(),但它给了我'\ xf0\x9f\x98\x84'.目前它输出的笑脸不是我想要的.encode('unicode_escape')给我一个UnicodeDecodeError.

笑脸作为字符串传递给python中的类方法.即"我很高兴"

感谢是否有人可以提供帮助.

对不起大笑脸.降价似乎在这里不起作用.

Ign*_*ams 19

>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604
Run Code Online (Sandbox Code Playgroud)


You*_*Tan 12

我找到了问题的解决方案.

我写了以下代码:

#convert to unicode
teststring = unicode(teststring, 'utf-8')

#encode it with string escape
teststring = teststring.encode('unicode_escape')
Run Code Online (Sandbox Code Playgroud)

  • 你能不能在python中提到这些功能的包名. (6认同)

ult*_*ine 8

另一个解决方案是在此处使用简称并使用字符串文字打印它们\N

print('\N{grinning face with smiling eyes}')
Run Code Online (Sandbox Code Playgroud)


小智 6

只需添加

# -*- coding: UTF-8 -*-
Run Code Online (Sandbox Code Playgroud)

在您的代码中,您将能够打印Unicode字符


Emm*_*mma 5

此代码可能会帮助您了解如何简单地打印表情符号:

代码

# -*- coding: UTF-8 -*-
import re

# string = 'Anything else that you wish to match, except URLs http://url.org'
string = 'Anything else that you wish to match'
matches = re.search(r'^(((?!http|https).)+)$', string)
if matches:
    print(matches.group(1)+ " is a match  ")
else: 
    print(' Sorry! No matches! Something is not right!')
Run Code Online (Sandbox Code Playgroud)

带 URL 的字符串输出

 Sorry! No matches! Something is not right!
Run Code Online (Sandbox Code Playgroud)

不带 URL 的字符串输出

Anything else that you wish to match is a match  
Run Code Online (Sandbox Code Playgroud)