python 去除文本中的中文、中文符号

删除中文及中文符号的方法,网上很多是python2 的写法,现在将python3的用法记录如下。

知识点
- 正则表达式
- 中文字符库zhon.hanzi,使用命令行pip3 install zhon 安装即可
#!/usr/bin/env python3
# encoding: utf-8
# coding style: pep8
# ====================================================
#   Copyright (C)2020 All rights reserved.
#
#   Author        : xxx
#   Email         : xxx@gmail.com
#   File Name     : check_chinese_and_symbol.py
#   Last Modified : 2020-06-03 18:11
#   Description   :
#
# ====================================================

import sys
# import os

import re

from zhon.hanzi import punctuation
from zhon.hanzi import characters

def lm_find_unchinese(file):
    pattern = re.compile(r'[\u4e00-\u9fa5]')
    unchinese = re.sub(pattern,"",file) #排除汉字
    unchinese = re.sub('[{}]'.format(punctuation),"",unchinese) #排除中文符号
    #print("unchinese:",unchinese)
    return unchinese


def lm_find_chinese(m_str):
    pattern = re.compile(r'[^\u4e00-\u9fa5]')
    chinese = re.sub(pattern, '', m_str)
    print("chinese:",chinese)

def lm_find_chinese_symbol(m_str):
    t_symbol = re.findall("[{}]".format(punctuation),m_str)
    print("chinese symbols:",t_symbol)

def lm_find_chinese_and_symbol(m_str):
    lm_find_unchinese(m_str)
    lm_find_chinese(m_str)
    lm_find_chinese_symbol(m_str)

def lm_delete_chinese_and_symbol(m_str):
    print("delete chinese and symbol")
    
# 测试用例
def test():
    fp = open("./CenterCrop.frag","r+")
    content = fp.read()
    print("查找到的中文符号:",re.findall("[{}]".format(punctuation),content))
    print("中文汉字:",re.findall("[{}]".format(characters),content))
    lm_find_chinese(content)
    unchinese =  lm_find_unchinese(content)
    fp.seek(0,0)
    fp.truncate()
    fp.write(unchinese)
    fp.close()

def main(argv=None):
#    lm_find_chinese_and_symbol(line)
    print("main")

if __name__ == "__main__":
    sys.exit(main())
  • 批量删除文本中的中文、中文符号
#!/usr/bin/env python3
# encoding: utf-8
# coding style: pep8
# ====================================================
#   Copyright (C)2020 All rights reserved.
#
#   Author        : xxx
#   Email         : xxx@gmail.com
#   File Name     : lm_find_files.py
#   Last Modified : 2020-06-04 11:03
#   Description   :
#
# ====================================================

import sys
import os

import check_chinese_and_symbol as chinese

def lm_find_files(path, target, result):
    """
    Basic Description:
            查找目录中指定类型的所有文件
            
    Detailed Description:
        
    Args:
        path: 查找的目录
		target: 目标文件类型,比如".json"
		result: 找到的目标文件列表
    Returns:
		result: 找到的目标文件列表
    Raises:
        exceptions
    """
    files = os.listdir(path);
    for f in files:
        npath = path + '/' + f
        if(os.path.isfile(npath)):
            if(os.path.splitext(npath)[1] in target):
                normal_path = os.path.normpath(os.path.abspath(npath))
                #print(normal_path)
                result.append(normal_path)
        if(os.path.isdir(npath)):
            if (f[0] == '.'):
                pass
            else:
                lm_find_files(npath, target, result)
    return result
    
def main(argv=None):
    result=[]
    #lm_find_files("./",".json",result)
    lm_find_files(sys.argv[1],[".frag",".vert",".lua"],result)
    #print(result)
    for f in result:
        print(f)
        fp = open(f,"r+")
        content = fp.read()
        unchinese = chinese.lm_find_unchinese(content)
        fp.seek(0,0)
        fp.truncate()
        fp.write(unchinese)
        fp.close()

if __name__=="__main__":
    sys.exit(main())
  • 8
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值