4

python3去除emoji表情符号

 2 years ago
source link: https://www.liurongxing.com/python3emoji.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

python3去除emoji表情符号

作者: JavasBoy 分类: Python 发布时间: 2021-02-20 17:15 ė 1472 views 6 没有评论

最近项目中使用load file导入文件到低版本MySQL中遇到卡住问题,最终问题为文本文件中某些行包含了emoji表情符号。所以使用python去掉这个emoji符号,然后再导出数据库。

需要安装如下模块:

https://pypi.org/project/demoji/

安装完demoji模块后需要手动下载emoji json文件,如果服务器无法上网,可以本地电脑运行,然后提取这个codes.json文件上传到服务器对应的目录。

>>> import demoji
>>> demoji.download_codes()
Downloading emoji data ...
... OK (Got response in 0.14 seconds)
Writing emoji data to /Users/brad/.demoji/codes.json ...
... OK

代码如下:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import demoji
file_path = "org_loadfile.txt"
final_fine = "loadfile.txt"
# 如果最终文件存在,则删除
if os.path.exists(final_fine):
os.remove(final_fine)
with open(file_path, 'r') as file:
for line in file:
# print(demoji.replace(line, "__"))
# 替换表情符号为 空
rap_line = demoji.replace(line, "")
# 写入文件
with open(final_fine, 'a') as f:
f.write(rap_line)

发表评论 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

评论

显示名称 *

电子邮箱地址 *

网站网址


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK