Python 过滤字符串中的emoji

使用python环境过滤文本当中的emoji表情

在使用spark处理用户评论时发现大量的ermoji无效表情,几乎不能作为有效评论,所以需要删除其中的无用emoji表情

方法一

需要先导入 emoji 包,然后通过写好的 give_emoji_free_text 函数操作
pip install emoji
import emoji

def give_emoji_free_text(text):
    allchars = [str for str in text.decode('utf-8')]
    emoji_list = [c for c in allchars if c in emoji.UNICODE_EMOJI]
    clean_text = ' '.join([str for str in text.decode('utf-8').split() if not any(i in str for i in emoji_list)])
    return clean_text

temp = u"it's okay love you \u2764\ufe0f\u2665\ufe0f\u2665\ufe0f\u2665\ufe0f"
temp1 = u'nice\U0001f60a\U0001f60a'
result = give_emoji_free_text(temp.encode('utf8'))

temp 的输出是已经去掉了字符串中原有的emoji

temp  (u"it's okay love you")
temp1 (u'nice\U0001f60a\U0001f60a')

但是对于temp1的例子输出则不能去除emoji,因为emoji字符与单子紧紧相连,在解码的时候会因为emoji编码长度与单词不同,而将一个emoji的编码分开成两个,导致无法在emoji.UNICODE_EMOJI编码序列中识别

所以需要使用方法二

方法二

emoji.get_emoji_regexp().sub(r'', temp1.encode('utf8').decode('utf8'))

方法二可以直接对temp、temp1的情况都去除掉其中的emoji

nice
it's okay love you 
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值