删除简单的表情符号 python

Remove simple emoji python

如何删除文本中的表情符号:

'ca va toi ;-) ?'

我使用了很多关于删除表情符号的解决方案,但没有任何效果。 我想要这样的输出:

ca va toi ?

我不想删除标点符号,只想删除那些构成表情符号的标点符号。 谢谢

以下应该适合您。您可以添加其他规则以使其更好地泛化。

x = 'ca va toi ;-) ?'

x = x.replace(';-)', '')
x = x.replace(';-(', '')
x = x.replace(';-|', '')
x = x.replace(';-D', '')

'ca va toi ?'

如果你也想清除所有标点符号,你可以执行以下操作

x = 'ca va toi ;-) ?'

''.join([i for i in x if (i >= 'a' and i<='z') or (i >= 'A' and i<='Z') or i == ' ')

'ca va toi '

一个简单的方法是指定可能的表情符号列表。

emoji_list = [";-)", ":)"]

然后删除字符串中出现的那些字符串。

# A dictionary with your emojis or any combination of characters you want to get rid of.
emoji_list = [";-)", ":)"]

# Your input string
string = 'ca va :) toi ;-) ?'

# Split the string into a list of substrings.
string_list = string.split()

# Using list comprehension, create a new list that excludes the emoji_list items.
clear_string = [string for string in string_list if string not in emoji_list]

# ALTERNATIVE a cleaner way is to use higher-order function filter to filter out the emojis.
clear_string = filter(lambda x: x not in emoji_list, string_list)

# Join the list into a string again.
output = " ".join(clear_string)

print(output)

您可以利用 python 中的列表推导来创建排除 emoji_list 中定义的子字符串的列表。另一种方法是使用 higher-order 函数 filter 过滤掉那些表情符号。

然后您会得到一个新列表,其中排除了您在 emoji_list 中定义的那些子字符串,然后您只需将列表连接到一个字符串即可获得所需的结果。

注意: 这是一种非常简单的方法,很容易 return 误报(即子字符串被视为表情符号,但实际上不是)。这些假设或案例不在此解决方案中。