Python 如何将数据集中存在的所有表情符号放入新文件中?

Python 如何将数据集中存在的所有表情符号放入新文件中?,python,emoji,Python,Emoji,如何将数据集中存在的所有表情符号放入新文件中 import emoji import re test_list=['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 emoj

如何将数据集中存在的所有表情符号放入新文件中

import emoji
import re

test_list=['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)
导入表情符号
进口稀土

test_list=['以下是如何迭代
提取emojis(test_list)
返回的列表,并将列表中的每个项目写入文件:

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)
编辑:如果您不想在文件中添加任何重复的表情符号,您首先需要读取文件的内容,然后在添加之前检查每个表情符号是否已经存在:


你的代码中有什么错误?你的代码运行正常,伙计。不是错误,但我如何才能将表情放入文件而不重复呢?这将给我一个列表,其中包含repetitions@zakariazakaria你能解释一下你的意思吗?你的意思是每次运行代码时都要覆盖文件而不是添加到文件中吗?我想要和但是如果表情符号已经存在于文件中,则无需再次添加它,例如,在我的第一条注释中:),在第二条注释中:p和第三条注释:)结果将是:)和:p“是的,这就是我想要的,如果我想让列表行在line@zakariazakaria然后使用
myfile.write(emoji+'\n')
而不是
myfile.write(emoji)
\n
是换行符)