【发布时间】:2021-03-12 14:21:48
【问题描述】:
我正在尝试创建一个函数,以便将完整的字符串传递给它,它会找到并返回任何存在的表情符号。例如,如果有 2 个表情符号,它应该返回两个。我该怎么做?
目前,我只能理解如何检查一个特定的表情符号。这是我用来检查的函数:
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
虽然这给了我们一个计数,但我不确定如何修改它以获取所有表情符号。
【问题讨论】:
-
stackoverflow.com/a/62898106/1554386 给出了一个不使用外部库的解决方案。
标签: python python-3.x emoji