Consider the following list:
a_list = ['π€ π me asΓ, bla es se π ds πππ']
How can I extract in a new list all the emojis inside a_list
?:
new_lis = ['π€ π π π π π']
I tried to use regex, but I do not have all the possible emojis encodings.
Emojis can also be implemented by using the emoji module provided in Python. To install it run the following in the terminal. emojize() function requires the CLDR short name to be passed in it as the parameter.
You can use the emoji
library. You can check if a single codepoint is an emoji codepoint by checking if it is contained in emoji.UNICODE_EMOJI
.
import emoji
def extract_emojis(s):
return ''.join(c for c in s if c in emoji.UNICODE_EMOJI['en'])
I think it's important to point out that the previous answers won't work with emojis like π¨βπ©βπ¦βπ¦ , because it consists of 4 emojis, and using ... in emoji.UNICODE_EMOJI
will return 4 different emojis. Same for emojis with skin color like π
π½.
Include the emoji
and regex
modules. The regex module supports recognizing grapheme clusters (sequences of Unicode codepoints rendered as a single character), so we can count emojis like π¨βπ©βπ¦βπ¦
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['en'] for char in word):
emoji_list.append(word)
return emoji_list
with more emojis with skin color:
line = ["π€ π me asΓ, se π ds πππ hello π©πΎβπ emoji hello π¨βπ©βπ¦βπ¦ how are π you todayπ
π½π
π½"]
counter = split_count(line[0])
print(' '.join(emoji for emoji in counter))
output:
π€ π π π π π π©πΎβπ π¨βπ©βπ¦βπ¦ π π
π½ π
π½
If you want to include flags, like π΅π° the Unicode range would be from π¦ to πΏ, so add:
flags = regex.findall(u'[\U0001F1E6-\U0001F1FF]', text)
to the function above, and return emoji_list + flags
.
See this answer to "A python regex that matches the regional indicator character class" for more information about the flags.
emoji
versionsto work with emoji >= v1.2.0 you have to add a language specifier (e.g. en
as in above code):
emoji.UNICODE_EMOJI['en']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With