将python表情符号打印为unicode字符串 [英] print python emoji as unicode string

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

问题描述

我一直在尝试将输出为'\U0001f604'而不是笑脸,但它不会输出



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



将笑脸作为字符串传递给python中的类方法。



code('unicode_escape')给了我一个UnicodeDecodeError。即我很高兴



感谢任何人有帮助。



抱歉,笑脸很大。降价似乎在这里不起作用。

解决方案

我找到了解决问题的方法。



我编写了以下代码:

 #转换为unicode 
teststring = unicode( teststring,'utf-8')

#使用字符串转义
对其进行编码teststring = teststring.encode('unicode_escape')


I've been trying to output as '\U0001f604' instead of the smiley but it doesn't seem to work.

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.

The smiley was passed as a string to a class method in python. i.e. "I am happy "

Appreciate if anyone could help.

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

解决方案

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')

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

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