使用 python 在文本中查找表情符号

问题描述 投票:0回答:4

你好,我正在尝试使用 python 2.7 查找下载的推文中的所有表情符号

我尝试过使用以下代码:

import os
import codecs
import emoji
from nltk.tokenize import word_tokenize

def extract_emojis(token):
    emoji_list = []
    if token in emoji.UNICODE_EMOJI:
        emoji_list.append(token)
    return emoji_list

for tweet in os.listdir(tweets_path):
    with codecs.open(tweets_path+tweet, 'r', encoding='utf-8') as input_file:
        line = input_file.readline()
        while line:
            line = word_tokenize(line)
            for token in line:
                print extract_emojis(token)

            line = input_file.readline()

但是我只得到空列表,而不是表情符号。如果我收到以下推文

schuld van de sossen 😡 SP.a: wij hebben niks gedaan 😴 Groen: we gaan energie VERBIEDEN!

代码的输出是

[]

而不是所需的输出:

[😡, 😴]

有什么帮助吗? 谢谢!

python emoji
4个回答
1
投票

确保您的文本已使用 utf-8 进行解码

text.decode('utf-8')

从文本中找到所有表情符号,您必须逐字符分隔文本

[str for str in decode]

将所有表情符号保存在列表中

[c for c in allchars if c in emoji.UNICODE_EMOJI]

类似这样的:

import emoji
text     = "🤔 🙈 lorum ipsum 😌 de 💕👭👙"
decode   = text.decode('utf-8')
allchars = [str for str in decode]
list     = [c for c in allchars if c in emoji.UNICODE_EMOJI]
print list

[u'\U0001f914', u'\U0001f648', u'\U0001f60c', u'\U0001f495', 你'\U0001f46d',你'\U0001f459']

要找回您的表情符号,请尝试这个


1
投票

这适用于 python 2 -

x = "schuld van de sossen 😡 SP.a: wij hebben niks gedaan 😴 Groen: we gaan energie VERBIEDEN!"
[i for i in x.split() if unicode(i, "utf-8") in emoji.UNICODE_EMOJI]

# OP
['\xf0\x9f\x98\xa1', '\xf0\x9f\x98\xb4']

0
投票

我们可以通过多种方式从 Python 中的字符串中提取表情符号。

其中最突出的是使用emoji库。

如果您正在处理文件,请确保您使用编码 utf-8 读取该文件(同时也保存 utf-8-sig

这里将展示如何列出字符串中存在的所有表情符号以及编号。字符串中表情符号的数量以及每个表情符号的类型

代码:

#import required libraries
import emoji
from emoji import UNICODE_EMOJI

#getting all emojis as lists
all_emojis = list(UNICODE_EMOJI.keys())

#defining sentence
sentence = "schuld van de sossen 😡 SP.a: wij hebben niks gedaan 😴 Groen: we gaan energie VERBIEDEN!"

#getting Emoji Count
emoji_count = sum([sentence.count(emoj) for emoj in UNICODE_EMOJI])
#listing all Emojis
listed_emojis = ','.join(re.findall(f"[{''.join(all_emojis)}]", str(sentence)))
#listing all Emoji Types
emoji_types = ','.join([UNICODE_EMOJI[detect_emoji].upper()[1:-1] for detect_emoji in listed_emojis.split(',')])

#Displaying Sentence, Emoji Count, Emojis and Emoji Types
print(f"Sentence: {sentence}\nListed Emojis: {listed_emojis}\nCount: {emoji_count}\nEmoji Types: {emoji_types}")

输出:

Sentence: schuld van de sossen 😡 SP.a: wij hebben niks gedaan 😴 Groen: we gaan energie VERBIEDEN!
Listed Emojis: 😡,😴
Count: 2
Emoji Types: POUTING_FACE,SLEEPING_FACE

我希望这会有所帮助..如果有人有疑问,请写在这里。我会尝试修复..:)


0
投票

试试这个:

import emoji

text = "Hello! 👋 How are you doing today? 😊 It’s a beautiful day, isn’t it? 🌞 Don’t forget to take a break and enjoy a cup of coffee ☕ or tea 🍵. Remember, you’re doing great! 👍 Keep up the good work! 💪"
emojis = [c for c in text if c in emoji.EMOJI_DATA]

print(emojis)

输出:

['👋', '😊', '🌞', '☕', '🍵', '👍', '💪']
© www.soinside.com 2019 - 2024. All rights reserved.