1

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.

7
  • Welcome to Stack Overflow. "But I am not getting desired result." What result do you get, and how is that different from the desired result? Commented Feb 9, 2022 at 0:08
  • 1
    line+1 makes no sense. line is a string, you can't add 1 to it. Commented Feb 9, 2022 at 0:10
  • Use readlines() to make a list of all the lines in the file. Then you can find the index of the line with tool> exit, and use a slice to get the lines you want: all_lines[index+1:-3] Commented Feb 9, 2022 at 0:11
  • The problem with @Barmar's suggestion is that the line with tool> exit would have to look exactly like this to work. The way you used in to detect that line made me think it might not be the case. Commented Feb 9, 2022 at 0:19
  • @Orius You can use a loop with in to find it. Commented Feb 9, 2022 at 0:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.