如何将数据集中存在的所有表情符号放入新文件中?
how can i put all the emojis that exist in my dataset into a new file?
如何将数据集中存在的所有表情符号放入新文件中?
import emoji
import re
test_list=[' thankyou']
创建提取表情符号的函数
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)
执行函数
extract_emojis(test_list)
以下是如何遍历 extract_emojis(test_list)
返回的列表,并将列表中的每一项写入文件:
with open('somefile.txt', 'a', encoding='utf-8') as myfile:
for emoji in extract_emojis(test_list):
myfile.write(emoji)
编辑:如果你不想在文件中添加任何重复的表情符号,你首先需要读取文件的内容,然后在添加之前检查每个表情符号是否已经存在:
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)
如何将数据集中存在的所有表情符号放入新文件中?
import emoji
import re
test_list=[' thankyou']
创建提取表情符号的函数
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)
执行函数
extract_emojis(test_list)
以下是如何遍历 extract_emojis(test_list)
返回的列表,并将列表中的每一项写入文件:
with open('somefile.txt', 'a', encoding='utf-8') as myfile:
for emoji in extract_emojis(test_list):
myfile.write(emoji)
编辑:如果你不想在文件中添加任何重复的表情符号,你首先需要读取文件的内容,然后在添加之前检查每个表情符号是否已经存在:
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)