Home > Software engineering >  How to parse lines after a certain text in python
How to parse lines after a certain text in python

Time:02-11

I am trying to parse a text file. I need certain line of it only. I need to search for "tool> exit" and parse all lines after that until 2nd last line. Here is the text I am trying to parse -

complete
line:567       finish;
tool> exit
  Finish Failed  Myname
       0      0   ABC_dog_ctl
       5      1   ABC_cator_t
      34      0   ABC_cator_t
       0      0   ABC_cator_t
  Total = 12,  Failing = 4
  summary at 70s
TOOL:   mytool

I am trying to parse only -

  Finish Failed  Myname
       0      0   ABC_dog_ctl
       5      1   ABC_cator_t
      34      0   ABC_cator_t
       0      0   ABC_cator_t
  Total = 12,  Failing = 4
  summary at 70s

I am trying something like -

my_list = []
for line_no, line in enumerate(Infile):
    if "tool> exit" in line:
        start_line = line_no
        my_list.append[line 1]

But I am not getting desired result. There are multiple instances of "TOOL" so I can't search it, but it's the last line so I think I can look for line above it always.

CodePudding user response:

Something like this?

my_list = []
reached_tool_exit = False
for line in Infile:
  if "tool> exit" in line:
    reached_tool_exit = True
  if reached_tool_exit:
    my_list.append(line)
my_list = my_list[:-1]

Edit:

Actually, you should use del my_list[-1] instead of my_list = my_list[:-1]. That's more efficient.

  • Related