返回表情符号名称而不是表情符号 [英] Return emoji name instead of emoji
问题描述
我有这个: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屋!