tum*_*eed 40 python python-3.x emoji
请考虑以下列表:
a_list = [' me así, bla es se ds ']
Run Code Online (Sandbox Code Playgroud)
如何在新列表中提取内部的所有表情符号a_list
?:
new_lis = [' ']
Run Code Online (Sandbox Code Playgroud)
我试图使用正则表达式,但我没有所有可能的表情符号编码.
Ped*_*lho 52
您可以使用该emoji
库.您可以通过检查单个代码点是否包含来表示它是否是表情符号代码点emoji.UNICODE_EMOJI
.
import emoji
def extract_emojis(str):
return ''.join(c for c in str if c in emoji.UNICODE_EMOJI)
Run Code Online (Sandbox Code Playgroud)
she*_*nzy 14
我认为必须指出,先前的答案不适用于???等表情符号。,因为它包含4个表情符号,并且using ... in emoji.UNICODE_EMOJI
将返回4个不同的表情符号。皮肤颜色像表情符号一样。
我的解决方案包括emoji
和regex
模块。regex模块支持识别字素簇(以单个字符呈现的Unicode代码点序列),因此我们可以计算表情符号,例如???。
import emoji
import regex
def split_count(text):
emoji_list = []
data = regex.findall(r'\X', text)
for word in data:
if any(char in emoji.UNICODE_EMOJI for char in word):
emoji_list.append(word)
return emoji_list
Run Code Online (Sandbox Code Playgroud)
测试(带有更多肤色的表情符号):
line = [" me así, se ds hello ? emoji hello ??? how are you today"]
counter = split_count(line[0])
print(' '.join(emoji for emoji in counter))
Run Code Online (Sandbox Code Playgroud)
输出:
? ???
Run Code Online (Sandbox Code Playgroud)
编辑:
如果要包括标志,例如Unicode范围将来自 至 ,因此添加:
flags = regex.findall(u'[\U0001F1E6-\U0001F1FF]', text)
Run Code Online (Sandbox Code Playgroud)
到上面的功能,和return emoji_list + flags
。
如果您不想使用外部库,则可以使用正则表达式和re.findall()
适当的正则表达式作为Python方式来查找表情符号:
In [74]: import re
In [75]: re.findall(r'[^\w\s,]', a_list[0])
Out[75]: ['', '', '', '', '', '']
Run Code Online (Sandbox Code Playgroud)
正则表达式r'[^\w\s,]'
是一个否定的字符类,它与非单词字符,空格或逗号的任何字符匹配。
正如我在评论中提到的那样,文本通常包含单词字符和标点符号,通过这种方法很容易处理,对于其他情况,您可以手动将它们添加到字符类中。注意,由于可以在字符类中指定字符范围,因此甚至可以使其更短,更灵活。
另一种解决方案是使用排除字符类来排除非表情符号字符,而不是使用接受表情符号([]
不带^
)的字符类。由于有许多具有不同unicode值的表情符号,因此您只需将范围添加到字符类中。如果您想匹配更多表情符号,这里是一个很好的参考,其中包含所有标准表情符号以及不同表情符号的相应范围http://apps.timwhitlock.info/emoji/tables/unicode:
小智 8
import emojis
new_list = emojis.get(' me así, bla es se ds ')
print(new_list)
output>>>{'', '', '', '', '', ''}
Run Code Online (Sandbox Code Playgroud)
小智 6
评分最高的答案并不总是有效。例如,将找不到标志表情符号。考虑字符串:
s = u'Hello \U0001f1f7\U0001f1fa hello'
Run Code Online (Sandbox Code Playgroud)
更好的做法是
import emoji
emojis_list = map(lambda x: ''.join(x.split()), emoji.UNICODE_EMOJI.keys())
r = re.compile('|'.join(re.escape(p) for p in emojis_list))
print(' '.join(r.findall(s)))
Run Code Online (Sandbox Code Playgroud)
第 1 步:确保您的文本已使用 utf-8 进行解码 text.decode(\'utf-8\')
第 2 步:从文本中找到所有表情符号,必须逐字符分隔文本[str for str in decode]
步骤 3:将所有表情符号保存在列表中[c for c in allchars if c in emoji.UNICODE_EMOJI]
完整示例如下:
>>> import emoji\n>>> text = " me as\xc3\xad, bla es se ds "\n>>> decode = text.decode(\'utf-8\')\n>>> allchars = [str for str in decode]\n>>> list = [c for c in allchars if c in emoji.UNICODE_EMOJI]\n>>> print list\n[u\'\\U0001f914\', u\'\\U0001f648\', u\'\\U0001f60c\', u\'\\U0001f495\', u\'\\U0001f46d\', u\'\\U0001f459\']\n
Run Code Online (Sandbox Code Playgroud)\n\n如果你想从文本中删除
\n\n>>> filtred = [str for str in decode.split() if not any(i in str for i in list)]\n>>> clean_text = \' \'.join(filtred)\n>>> print clean_text\nme as\xc3\xad, bla es se ds\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
25891 次 |
最近记录: |