python 删除多个文本里的指定行
比如有多个文本1.txt2.txt3.txt4.txt5.txt,里面的内容大概是:12124242----000012345678----111123456789---...
比如有多个文本1.txt 2.txt 3.txt 4.txt 5.txt,里面的内容大概是:
12124242----0000
12345678----1111
23456789----2222
34567890----3333
40404044----4444
现在新建个文本0.txt,如果我在文本里写上一行或是多行数据。例如:
12345678
23456789
34567890
就会自动到1.txt 2.txt 3.txt 4.txt 5.txt里面去匹配,如果某个文本跟上面的数据有匹配到的话,就把这一整行都给删除掉,并把删除的内容新建个文档储存。如果没有匹配到的话就忽略,每30秒检测一次。。
因为每次要删除若干行的话,都要每个文本去打开查询删除,太麻烦了,麻烦大虾赐教 展开
12124242----0000
12345678----1111
23456789----2222
34567890----3333
40404044----4444
现在新建个文本0.txt,如果我在文本里写上一行或是多行数据。例如:
12345678
23456789
34567890
就会自动到1.txt 2.txt 3.txt 4.txt 5.txt里面去匹配,如果某个文本跟上面的数据有匹配到的话,就把这一整行都给删除掉,并把删除的内容新建个文档储存。如果没有匹配到的话就忽略,每30秒检测一次。。
因为每次要删除若干行的话,都要每个文本去打开查询删除,太麻烦了,麻烦大虾赐教 展开
3个回答
2013-03-05
展开全部
代码基于python 2.6。功能已写成函数,用的简单语法,很好懂。
新文件文件名自动附加"_back"。不懂再问。
import os, time
def readKeys(fileName):
keys = []
f = open(fileName, "r")
while True:
line = f.readline()
if not line:
break
key = line.strip()
if key: keys.append(key)
f.close()
return keys
def processKeys(editFileName, backFileName, keys):
f = open(editFileName, "r")
lines = f.readlines()
f.close()
editLines = []
backLines = []
for line in lines:
found = False
for key in keys:
if line.startswith(key):
backLines.append(line)
found = True
break
if not found:
editLines.append(line)
if backLines:
f = open(editFileName, "w")
f.writelines(editLines)
f.close()
f = open(backFileName, "w")
f.writelines(backLines)
print 'modify',editFileName,'save',backFileName
if __name__ == '__main__':
keys = readKeys("0.txt")
fileList = ["1.txt", "2.txt", "3.txt", "4.txt", "5.txt"]
while True:
for fileName in fileList:
base, ext = os.path.splitext(fileName)
processKeys(fileName, base + "_back" + ext, keys)
print 'sleep 30 seconds'
time.sleep(30)
新文件文件名自动附加"_back"。不懂再问。
import os, time
def readKeys(fileName):
keys = []
f = open(fileName, "r")
while True:
line = f.readline()
if not line:
break
key = line.strip()
if key: keys.append(key)
f.close()
return keys
def processKeys(editFileName, backFileName, keys):
f = open(editFileName, "r")
lines = f.readlines()
f.close()
editLines = []
backLines = []
for line in lines:
found = False
for key in keys:
if line.startswith(key):
backLines.append(line)
found = True
break
if not found:
editLines.append(line)
if backLines:
f = open(editFileName, "w")
f.writelines(editLines)
f.close()
f = open(backFileName, "w")
f.writelines(backLines)
print 'modify',editFileName,'save',backFileName
if __name__ == '__main__':
keys = readKeys("0.txt")
fileList = ["1.txt", "2.txt", "3.txt", "4.txt", "5.txt"]
while True:
for fileName in fileList:
base, ext = os.path.splitext(fileName)
processKeys(fileName, base + "_back" + ext, keys)
print 'sleep 30 seconds'
time.sleep(30)
更多追问追答
追问
只运行一次就自动关闭了,显示为:
modify 1.txt save 1_back.txt
Traceback (most recent call last):
File "E:\测试\删除.py", line 47, in
processKeys(fileName, base + "_back" + ext, keys)
File "E:\测试\删除.py", line 16, in processKeys
f = open(editFileName, "r")
IOError: [Errno 2] No such file or directory: '5.txt'
而且麻烦大虾给注释一下啦,俺一点基础都没有
追答
你的目录下 没有5.txt吧?
你检查一下1.txt - 4.txt, 应该是已经修改过了。
等下贴注释。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import re
patt = re.compile(r'^(\d+).*')
def _ln2num(ln):
try:
return patt.match(ln).group(1)
except:
return ln.strip()
def prepair_exists(filename):
with open(filename, 'rt') as handle:
return set(map(_ln2num, handle))
exists = set()
for filename in ('1.txt', '2.txt', '3.txt', '4.txt'):
exists |= prepair_exists(filename)
with open('0.txt.' ,'rt') as handle:
rs = [filter(lambda ln: ln.strip() not in exists, handle]
with open('0.txt', 'wt') as handle:
handle.writelines(rs)
patt = re.compile(r'^(\d+).*')
def _ln2num(ln):
try:
return patt.match(ln).group(1)
except:
return ln.strip()
def prepair_exists(filename):
with open(filename, 'rt') as handle:
return set(map(_ln2num, handle))
exists = set()
for filename in ('1.txt', '2.txt', '3.txt', '4.txt'):
exists |= prepair_exists(filename)
with open('0.txt.' ,'rt') as handle:
rs = [filter(lambda ln: ln.strip() not in exists, handle]
with open('0.txt', 'wt') as handle:
handle.writelines(rs)
更多追问追答
追问
大哥,能加点注释不,俺菜鸟看不懂呢。。还有,俺的PY是2.6版本的,你上面的代码运行出错
追答
import re
patt = re.compile(r'^(\d+).*')
def _ln2num(ln):
""" 用正则表达式从行中提取前导数字串 """
try:
return patt.match(ln).group(1)
except:
return ln.strip()
def prepair_exists(filename):
""" 从文件中读取各行的前导数字串, 组成set """
handle = open(filename, 'rt')
return set(map(_ln2num, handle))
# 从给定的文件列表读取各个文件的前导数字串
# 组成供后期判断用的 "exists" 集合
exists = set()
for filename in ('1.txt', '2.txt', '3.txt', '4.txt'):
exists |= prepair_exists(filename)
# 读取"0.txt"
handle = open('0.txt' ,'rt')
# 不在"exists"集合中的各行组成列表 rs
rs = filter(lambda ln: ln.strip() not in exists, handle)
handle.close()
# 将"rs"回写"0.txt"
handle = open('0.txt', 'wt')
handle.writelines(rs)
handle.close()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我也是初学,等会给你发个试着用用
更多追问追答
追问
谢大哥。要完整点哈,还得注释一下。俺是啥都不懂的。。
追答
import os
import time
DataPath = r'..\data' #你说的'1.txt,2.txt...'放置的目录,代码自动扫描该目录所有文件
OutDataPath=r'..\out' #去除指定字符串后,保存的文件目录
SourceFile = r'..\source\0.txt'
source_object = open(SourceFile)
source_of_lines = source_object.readlines()
def saveData(file,list):#写入内容到文件
tempfile = open(os.path.join(OutDataPath,file), 'wt')
tempfile.writelines(list)
tempfile.close()
def main():
try:
files = os.listdir(DataPath)#得到文件列表(1.txt 2.txt 3.txt...)
for file in files:
file_object = open(os.path.join(DataPath, file)).readlines()
temp_list = open(os.path.join(DataPath, file)).readlines()
for fileline in file_object:
for sourcestr in source_of_lines:
if sourcestr.strip() in fileline:
print file,'====remove===', fileline
temp_list.remove(fileline)#这里是删除这一行
saveData(file,temp_list)
except Exception,e:
print 'error',e
if __name__=='__main__':
while(1):
main()
time.sleep(30)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询