# -*- coding: cp936 -*-
import re
import string
# 方法一
""""删除两个标记字符中间的字符串内容"""
def deletesACharacterBetweenTwoSpecifiedCharacters(startStr,endStr,filename,saveName):
# 打开文件获取文件里面的内容
file = open(filename +'.txt')
lines = file.readlines()
string = ''.join(lines)
# 字符串截取,截取字符串中的两个指定的标识字符中间的内容
# result = re.findall("aa(.*?)bb",string)
result = re.findall(startStr+"(.*?)"+endStr, string)
# result 是筛选出来的中间字符列表格式
print(result)
# 中间字符转换成字符串
zhongjianStr = "".join(result)
# 中间字符拼接成sub函数可以批量截取的格式
zhongjianStr = "["+zhongjianStr+"]"
# print(re.sub(zhongjianStr,'',string))
zhongjianStr = re.sub(zhongjianStr,'',string)
zhongjianStr = re.sub(startStr+endStr,'',zhongjianStr)
# 将截取的字符写入文件里面
# 创建文件名 文件可读写性 编码格式
save = open(saveName +'.txt',mode='w',encoding='utf-8')
save.write(zhongjianStr)
save.close()
# 函数使用
# deletesACharacterBetweenTwoSpecifiedCharacters('aa','bb','ceshi','result')
# 方法二
"""""""""可以简写删除的字符'a-zA-Z0-9读书'"""""""""
# 这样使用会将文本里所有的有读和书的字都删掉,他删掉的不是词语读书,而是删的字读和书
def batchDelStrAndSaveFile(searchStr,fileName,saveName):
print(batchDelStrAndSaveFile)
file = open(fileName+'.txt')
lines = file.readlines()
string = ''.join(lines)
needDelStr = '['+searchStr+']'
getWantStr = re.sub(needDelStr,'',string)
# 将截取的字符写入文件里面
# 创建文件名 文件可读写性 编码格式
save = open(saveName +'.txt',mode='w',encoding='utf-8')
save.write(getWantStr)
save.close()
#函数使用
batReFile = 'mingchao'
batSaFile = 'batSaveStr'
# 可以简写删除不想要的字符
# 例子'a-zA-Z0-9'
# batSearcStr = 'a-z'
# batchDelStrAndSaveFile(batSearcStr,batReFile,batSaFile)
# def checkFile (keywords,text):
# return re.sub('|'.join(keywords),'***',text)
# keywords = ('暴力','色情')
# text = '这句abc话a不c含v暴力,也不含色情,暴'
# print(checkFile(keywords ,text))
# 方法三
""""""""" 拼接成需要的字符格式"'a','b','c'"""""
# def formatTheRequiredCharacters(listChar):
# return "'"+"','".join(listChar)+"'"
"""""""""# 删除指定的字符,删除字符不支持'a-z'这样书写"""""""""
def getDelStrAndSaveStr(searchKeywords,readFileName,saveFileName):
print(getDelStrAndSaveStr)
file = open(readFileName + '.txt')
lines = file.readlines()
string = ''.join(lines)
getWantStr = re.sub('|'.join(searchKeywords),'',string)
# 将截取的字符写入文件里面
# 创建文件名 文件可读写性 编码格式
save = open(saveFileName + '.txt',mode = 'w',encoding = 'utf-8')
save.write(getWantStr)
# print(getWantStr)
save.close()
# print ([chr(i) for i in range(65,91)])#所有大写字母
# print ([chr(i) for i in range(97,123)])#所有小写字母
# print ([chr(i) for i in range(48,58)])#所有数字
refile = 'shangxu'
safile = 'getSingle'
# searc = ("a","b")
xiaoXieZiMu = [chr(i) for i in range(97,123)]
daXieZiMu = [chr(i) for i in range(65,91)]
shuzi = [chr(i) for i in range(48,58)]
cizu = ['爱下电子Txt版阅读,下载和分享更多电子请访问','手机访问','未完待续','求订阅','求月票','求推荐','求推荐票'
'小说','本书籍由 http://www.qingkan9.com/ 提供下载','/','[',']']
searc = cizu + xiaoXieZiMu+daXieZiMu+shuzi
getDelStrAndSaveStr(searc,refile,safile)
def deletesACharacterBetweenTwoSpecifiedCharacters(startStr,endStr,filename,saveName):
# 打开文件获取文件里面的内容
file = open(filename +'.txt')
lines = file.readlines()
string = ''.join(lines)
# 字符串截取,截取字符串中的两个指定的标识字符中间的内容
# result = re.findall("aa(.*?)bb",string)
result = re.findall(startStr+"(.*?)"+endStr, string)
# result 是筛选出来的中间字符列表格式
# print(result)
# 中间字符转换成字符串
# zhongjianStr = "".join(result)
# 中间字符拼接成sub函数可以批量截取的格式
# zhongjianStr = "["+zhongjianStr+"]"
# print(re.sub(zhongjianStr,'',string))
# print(result)
zhongjianStr = re.sub('|'.join(result),'',string)
zhongjianStr = re.sub(startStr+endStr,'',zhongjianStr)
# 将截取的字符写入文件里面
# 创建文件名 文件可读写性 编码格式
save = open(saveName +'.txt',mode='w',encoding='utf-8')
save.write(zhongjianStr)
save.close()
# 函数使用
deletesACharacterBetweenTwoSpecifiedCharacters('aaa','bbb','ceshi','result')
代码有点low,还在探索中,希望大神指教,寻找更优的方法。
python 批量删除两个指定字符中间的字符串,字符串,删除指定字符
最新推荐文章于 2024-06-29 18:00:00 发布