我正在尝试创建一个函数,这样我向它传递一个完整的字符串,它就会找到并返回任何存在的表情符号。例如,如果有两个表情符号,它应该同时返回两个。我如何做到这一点呢?
目前,我只能了解如何检查一个特定的表情符号。这是一个我用来检查它的函数:
def check(string):
if '✅' in string:
print('found', string)我想在不指定任何表情符号的情况下做到这一点,只是寻找所有的表情符号。我考虑过from emoji import UNICODE_EMOJI。
import emoji
import regex
def split_count(text):
emoji_counter = 0
data = regex.findall(r'\X', text)
for word in data:
if any(char in emoji.UNICODE_EMOJI for char in word):
emoji_counter += 1
# Remove from the given text the emojis
text = text.replace(word, '')
words_counter = len(text.split())
return emoji_counter, words_counter虽然这给了我们一个计数,但我不确定如何修改它来获得所有的表情符号。
发布于 2020-12-01 00:39:50
你可以检查这封信是否为emoji.UNICODE_EMOJI格式
import emoji
def get_emoji_list(text):
return [letter for letter in text if letter in emoji.UNICODE_EMOJI]
print(get_emoji_list('✅aze✅'))
# ['✅', '✅']如果您想要一组独特的表情符号,请在函数中更改您的理解,以创建set而不是list
import emoji
def get_emoji_set(text):
return {letter for letter in text if letter in emoji.UNICODE_EMOJI}
print(get_emoji_list('✅aze✅'))
# {'✅'}发布于 2020-12-01 00:38:42
这个emoji_finder方法yields找到表情符号的单词。因此,可以将generator object转换为列表,并随心所欲地使用它。
import emoji
import regex
def emoji_finder(text):
emoji_counter = 0
data = regex.findall(r'\X', text)
for word in data:
if any(char in emoji.UNICODE_EMOJI for char in word):
emoji_counter += 1
text = text.replace(word, '')
yield word
print(list(split_count(stringWithEmoji))) #prints all the emojis in stringWithEmojihttps://stackoverflow.com/questions/65077353
复制相似问题