打印python表情符作为unicode字符串

时间:2022-01-17 23:14:23

I've been trying to output 打印python表情符作为unicode字符串 as '\U0001f604' instead of the smiley but it doesn't seem to work.

我一直在尝试输出'\U0001f604'而不是笑脸,但似乎不起作用。

i tried using repr() but it gives me this '\xf0\x9f\x98\x84'. Currently it outputs as the smiley which is not what I wanted. encode('unicode_escape') gives me a UnicodeDecodeError.

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

The smiley was passed as a string to a class method in python. i.e. "I am happy 打印python表情符作为unicode字符串"

smiley作为字符串传递给python中的类方法。即。“我很高兴”

Appreciate if anyone could help.

如果有人能帮忙,请感激。

Sorry for the big smiley. The markdown doesn't seem to work here.

为那个大笑脸道歉。降价似乎在这里行不通。

4 个解决方案

#1


13  

>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604

#2


10  

I found the solution to the problem.

我找到了解决这个问题的办法。

I wrote the following code:

我写了以下代码:

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

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

#3


3  

Just add

只需添加

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

into your code and you will be able to print Unicode characters

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

#4


1  

If this is for debugging purposes, you could use %r as the format specifier.

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

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

#1


13  

>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604

#2


10  

I found the solution to the problem.

我找到了解决这个问题的办法。

I wrote the following code:

我写了以下代码:

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

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

#3


3  

Just add

只需添加

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

into your code and you will be able to print Unicode characters

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

#4


1  

If this is for debugging purposes, you could use %r as the format specifier.

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

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