我该如何解析以下日志?

dan*_*els 2 python parsing

我需要以下列格式解析日志:

===== Item 5483/14800  =====
This is the item title
Info: some note
===== Item 5483/14800 (Update 1/3) =====
This is the item title
Info: some other note
===== Item 5483/14800 (Update 2/3) =====
This is the item title
Info: some more notes
===== Item 5483/14800 (Update 3/3) =====
This is the item title
Info: some other note
Test finished. Result Foo. Time 12 secunds.
Stats: CPU 0.5 MEM 5.3
===== Item 5484/14800  =====
This is this items title
Info: some note
Test finished. Result Bar. Time 4 secunds.
Stats: CPU 0.9 MEM 4.7
===== Item 5485/14800  =====
This is the title of this item
Info: some note
Test finished. Result FooBar. Time 7 secunds.
Stats: CPU 2.5 MEM 2.8
Run Code Online (Sandbox Code Playgroud)

我只需要提取每个项目的标题(=====项目5484/14800 =====之后的下一行)和结果.
所以我需要只保留项目标题的行和该标题的结果,并丢弃其他所有内容.
问题是,有时一个项目有注释(格言3),有时结果显示没有附加注释,所以这使得它很棘手.
任何帮助,将不胜感激.我在python中做解析器,但不需要实际的代码,但有些指向我怎么能得到这个?

LE:我正在寻找的结果就是抛弃其他所有东西,得到类似的东西:

('This is the item title','Foo')
then
('This is this items title','Bar')
Run Code Online (Sandbox Code Playgroud)

Ian*_*obs 5

1) Loop through every line in the log

    a)If line matches appropriate Regex:

      Display/Store Next Line as the item title.
      Look for the next line containing "Result 
      XXXX." and parse out that result for 
      including in the result set.
Run Code Online (Sandbox Code Playgroud)

编辑:现在添加了一点,我看到你正在寻找的结果.


Tri*_*ych 5

我知道你没有要求真正的代码,但这对于基于生成器的文本管理器来说是太大的机会了:

# data is a multiline string containing your log, but this
# function could be easily rewritten to accept a file handle.
def get_stats(data):

   title = ""
   grab_title = False

   for line in data.split('\n'):
      if line.startswith("====="):
         grab_title = True
      elif grab_title:
         grab_title = False
         title = line
      elif line.startswith("Test finished."):
         start = line.index("Result") + 7
         end   = line.index("Time")   - 2
         yield (title, line[start:end])


for d in get_stats(data):
   print d


# Returns:
# ('This is the item title', 'Foo')
# ('This is this items title', 'Bar')
# ('This is the title of this item', 'FooBar')
Run Code Online (Sandbox Code Playgroud)

希望这很简单.请问您是否对上述工作原理有疑问.