Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 将python表情符号打印为unicode字符串_Python 2.7_Unicode_Emoji - Fatal编程技术网

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

Python 2.7 将python表情符号打印为unicode字符串,python-2.7,unicode,emoji,Python 2.7,Unicode,Emoji,我一直试图输出”我找到了问题的解决方案 >>> print u'\U0001f604'.encode('unicode-escape') \U0001f604 我编写了以下代码: #convert to unicode teststring = unicode(teststring, 'utf-8') #encode it with string escape teststring = teststring.encode('unicode_escape') 如果这是为了调

我一直试图输出
”我找到了问题的解决方案

>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604
我编写了以下代码:

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

#encode it with string escape
teststring = teststring.encode('unicode_escape')

如果这是为了调试,可以使用%r作为格式说明符

>>> print '%r' % u'\U0001f604'
u'\U0001f604'
加上

# -*- coding: UTF-8 -*-

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

另一种解决方案是使用名称别名并使用字符串文本打印它们
\N

print('\N{grinning face with smiling eyes}')

当前名称别名列表可在

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

代码
#-*-编码:UTF-8-*-
进口稀土
#string='您希望匹配的任何其他内容,URL除外http://url.org'
字符串='您希望匹配的任何其他内容'
matches=re.search(r'^(((?!http | https)。)+)$,字符串)
如果匹配:

print(matches.group(1)+”是一个匹配项。请在python中提及这些函数的包名。
unicode
是内置的,
.encode()
是字符串函数-无需导入任何packages@vijayathithya
bytes.decode(b'\xf0\x9f\x98\x84','utf8')
可能是上面的
unicode
调用所指的Python 3版本。如果你知道你有一个
bytes
对象,你可以直接调用
decode
来获得一个(unicode)
str
对象,例如
b'\xf0\x9f\x98\x84'。decode('utf8')
@SamMason yeh mate“出于某种原因的表情符号?很好地抓住了@JonathanSimon。事实证明,实际上应该使用的是‘名字别名’而不是‘短名字’。相应地更新了答案。对于竖起的名字别名是‘竖起的符号’。因此,
print('\N{thumbs-up-sign}')
工作正常。