简体   繁体   中英

How to extract specific lines from a file and save specific lines into each new file in python

I have a file which contains following lines.In this I want to read the lines between line which startswith forw and the line before endswith .txt. copy each set of extracted lines into separate new files using python script.

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

My output should be like:

forw l1tt activate 1
forw l1tt DeleteULPhCH 0
forw l1tt activate 1
forw l1tt DeleteCCB 0 1 0
forw l1tt DeleteDLPhCH 0

in one file.

and in another file it should be like:

forw l1tt setuecontext 100
forw l1tt DeleteCCB 65 1 0

I used the following python code: It is extracting only the first set of output.But I cant able to extract second set of output after given the break condition.Please anyone help me soon.

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()

Using a simple naive state machine you can do it like this:

#!/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 = []

Which produces the following output files:

$ 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

It's not quite exactly what you're after, but it's close. Hopefully you can modify this to suit your needs.

State machines are very useful!

It seems that he does not want to include the '.txt' lines into the created files

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

Creates files:

$ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM