获取字符串中的所有表情符号

Get all emojis in a string

我正在尝试创建一个函数,以便将完整的字符串传递给它,它会找到并 returns 存在的任何表情符号。例如,如果有 2 个表情符号,它应该 return 两个。我该怎么做?

目前,我只能了解如何检查一个特定的表情符号。这是我用来检查它的函数:

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

虽然这给了我们一个计数,但我不确定如何修改它以获得所有表情符号。

emoji_finder 方法 yield 是找到表情符号的词。所以 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 stringWithEmoji

您可以检查字母是否在 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✅'))
# {'✅'}