我有一个包含以下行的文件。在这里我想要读取以forw开头的行和结束于.txt之前的行之间的行。使用python脚本将每组提取的行复制到单独的新文件中。
forw l1tt DeleteULPhCH 0
forw l1tt activate 1
forw l1tt DeleteCCB 0 1 0
forw l1tt DeleteDLPhCH 0
BCH_CCB63.txt
DL_BCH_PhCh.txt
forw l1tt setuecontext 100
forw l1tt DeleteCCB 65 1 0
DL_BCH_PhCh.txt
我的输出应该是:
forw l1tt activate 1
forw l1tt DeleteULPhCH 0
forw l1tt activate 1
forw l1tt DeleteCCB 0 1 0
forw l1tt DeleteDLPhCH 0
在一个文件中。
在另一个文件中它应该是:
forw l1tt setuecontext 100
forw l1tt DeleteCCB 65 1 0
我使用了以下python代码:它只提取第一组输出。但是在给出中断条件后我无法提取第二组输出。请任何人尽快帮助我。
fin=open("script.txt","r")
fout=open("output.txt","w")
lines=fin.readlines()
for line in lines:
if re.search(r"(.*)(.txt)",line):
break
print line
fout.write(line)
fin.close()
fout.close()
答案 0 :(得分:0)
使用简单的天真状态机你可以这样做:
#!/usr/bin/env python
n = 0
output = []
state = 0 # 0 = start, 1 = forw
with open("foo.txt", "r") as f:
for line in f:
line = line.strip()
if "forw" in line:
state = 1
if state == 1:
output.append(line)
if ".txt" in line:
state = 0
with open("{0:d}.txt".format(n), "w") as outf:
outf.write("\n".join(output))
outf.write(line)
n += 1
output = []
生成以下输出文件:
$ cat 0.txt
forw l1tt DeleteULPhCH 0
forw l1tt activate 1
forw l1tt DeleteCCB 0 1 0
forw l1tt DeleteDLPhCH 0
BCH_CCB63.txtBCH_CCB63.txt
$ cat 1.txt
forw l1tt setuecontext 100
forw l1tt DeleteCCB 65 1 0
DL_BCH_PhCh.txtDL_BCH_PhCh.txt
这不完全是你所追求的,但它已经接近了。 希望您可以根据自己的需要进行修改。
状态机非常有用!
答案 1 :(得分:0)
他似乎不想将'.txt'行包含在创建的文件中
import re
n = 1
with open("script.txt","r") as my_file:
my_list = []
for line in my_file.readlines():
if not re.search(r"(.*)(.txt)",line):
my_list.append(line)
with open("output"+str(n)+".txt","w") as out_file:
for item in my_list:
out_file.write(item)
else:
if my_list:
my_list=[]
n += 1
创建文件:
$ cat output1.txt
forw l1tt DeleteULPhCH 0
forw l1tt activate 1
forw l1tt DeleteCCB 0 1 0
forw l1tt DeleteDLPhCH 0
$ cat output2.txt
forw l1tt setuecontext 100
forw l1tt DeleteCCB 65 1 0