Step by step:
#!/usr/bin/env python# coding: utf-8
def getLines(filename):
"""step 1: read from file into {"capture": .., "blocks": [..]}"""
with open(filename) as handle:
capture = handle.readline().strip()
handle.readline() # skip 2 line
handle.readline()
blocks = [[]]
for ln in handle:
if not ln.strip(): # empty line break a block
blocks.append([])
else:
blocks[-1].append(ln.strip('\n'))
return dict(
capture=capture,
blocks=blocks
)
def blockparser(block):
"""step 2: parser a block:"""
rule = block[-1]
idx = rule.index('*')
rows = [(ln[:idx], ln[idx:]) for ln in block[:-1]]
return rows
for block in getLines("file_in.txt")["blocks"]:
rows = blockparser(block)
print rows