Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split emoji from each other python?

Tags:

python

emoji

I need to split emoji from each other for example

EM = 'Hey 😷😷😷'
EM.split()

If we split it we will have

['Hey' ,'😷😷😷']

I want to have

['hey' , '😷' , '😷' , '😷']

and I want it to be applied to all emojis.

like image 709
Abi2021 Avatar asked Apr 19 '18 12:04

Abi2021


Video Answer


1 Answers

You should be able to use get_emoji_regexp from the https://pypi.org/project/emoji/, together with the usual split function . So something like:

import functools
import operator
import re

import emoji

em = 'Hey 😷😷😷'
em_split_emoji = emoji.get_emoji_regexp().split(em)
em_split_whitespace = [substr.split() for substr in em_split_emoji]
em_split = functools.reduce(operator.concat, em_split_whitespace)

print(em_split)

outputs:

['Hey', '😷', '😷', '😷']

A more complex case, with family, skin tone modifiers, and a flag:

em = 'Hey πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘§πŸ‘¨πŸΏπŸ˜·πŸ˜·πŸ‡¬πŸ‡§'
em_split_emoji = emoji.get_emoji_regexp().split(em)
em_split_whitespace = [substr.split() for substr in em_split_emoji]
em_split = functools.reduce(operator.concat, em_split_whitespace)

for separated in em_split:
    print(separated)

outputs:

Hey
πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘§
πŸ‘¨πŸΏ
😷
😷
πŸ‡¬πŸ‡§

(I think something's up with using print on a list with the family emoji, hence printing each item of the list separately. Printing family emoji, with U+200D zero-width joiner, directly, vs via list)

like image 117
Michal Charemza Avatar answered Nov 07 '22 14:11

Michal Charemza