简体   繁体   English

如何解析python中某个文本后的行

[英]How to parse lines after a certain text in python

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.我需要搜索“tool> exit”并解析之后的所有行,直到倒数第二行。 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.有多个“TOOL”实例,所以我无法搜索它,但它是最后一行,所以我想我总能找到它上面的行。

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] .实际上,您应该使用del my_list[-1]而不是my_list = my_list[:-1] That's more efficient.这样更有效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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