返回表情符号名称,而不是表情符号 [英] Return emoji name instead of emoji

查看:134
本文介绍了返回表情符号名称,而不是表情符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:Python 3中的'1⃣'(不带单引号)::one:。有没有办法我可以获取表情符号(如上面的表情符号)并打印相应的表情符号(在这种情况下:one :)名称?

I have this: '1⃣' (without the single quotes) in Python 3, which is :one:. Is there a way I could get the emoji (like the one above) and print the corresponding emoji (in this case :one:) name instead?

我从discord.py反应对象获取表情符号。

I'm getting the emoji from a discord.py reaction object.

推荐答案

在您的情况下,表情符号是两个字符的字符串。您可以通过获取字符串的第一个字符来获取数字:

In your case, that emoji is a two-character string. You can get the number by getting the first character of the string:

char = '1⃣'
print(char[0]) # 1

对于另一个不只是两个字符的表情符号,您可以使用 unicodedata 模块:

With another emoji that isn't just two characters, you can use the unicodedata module:

import unicodedata

char = '❤'
name = unicodedata.name(char)
print(name) # HEAVY BLACK HEART

在大多数情况下,表情符号的名称是unicode名称的最后一个字:

In most cases, the name of the emote is the last word of the unicode name:

import unicodedata

char = '1⃣'
name = unicodedata.name(char[0])
name = name.split(' ')[-1]
print(f':{name.lower()}:')
# :one:

这篇关于返回表情符号名称,而不是表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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