how can i put all the emojis that exist in my dataset into a new file ?

import emoji
import re

test_list=['  thankyou']

Create the function to extract the emojis

def extract_emojis(a_list):
emojis_list = map(lambda x: ''.join(x.split()), 
emoji.UNICODE_EMOJI.keys())
r = re.compile('|'.join(re.escape(p) for p in emojis_list))
aux=[' '.join(r.findall(s)) for s in a_list]
return(aux)

Execute the function

extract_emojis(test_list)

1 Answers

0
ghr On Best Solutions

Here's how to iterate over the list returned by extract_emojis(test_list), and write each item in the list to a file:

with open('somefile.txt', 'a', encoding='utf-8') as myfile:
    for emoji in extract_emojis(test_list):
        myfile.write(emoji)

Edit: if you don't want to add any duplicate emojis into the file, you first need to read the contents of the file, and then check if each emoji already exists before adding it:

with open("somefile.txt","r", encoding='utf-8') as myfile:
    filecontent = myfile.read()

with open('somefile.txt', 'a', encoding='utf-8') as myfile:
    for comment in extract_emojis(test_list):
      for emoji in comment:
        if emoji != ' ' and emoji not in filecontent:
          myfile.write(emoji)