<p>下面是一个简单的基于状态的解析器。它没有经过很好的测试,可能会对日志文件的格式做出一些不正确的假设。不过,它应该能帮助你开始。你知道吗</p>
<pre><code>def process_log(path):
contents = []
with open(path) as stream:
state = 0
line = ''
block = None
while line is not None:
try:
if not line:
line = next(stream)
except StopIteration:
line = None
else:
line = line.strip()
if state == 0:
if line.startswith(' ServiceDataBlock'):
block = []
state = 1
line = ''
continue
elif state >= 1:
if line.startswith('>'):
line = line.lstrip('> ')
if not line.startswith('From channel#'):
block.append(line)
if line.startswith('TP_UserData '):
state = 2
line = ''
continue
if state == 2:
contents.append(block)
state = 0
return contents
</code></pre>