I have written small program in Python which reads the following file and stores the result into a dictionary. I am getting expected output but I think it can be done in a better way. I am using Python 3.
Input
check_name : xa25
not run:
del_l6w_
dl_l4w_
dl_l22w_
de_l3w_
ckt_pw_
ckt_pw_
run:
inv_w_
buf_w_
End
Code
import collections
def main():
chk_cell = "input.txt"
chk_cell_data = collections.defaultdict(dict)
with open(chk_cell, "r") as fchk_cell:
check_name = ""
not_run_flag = False
run_flag = False
for data in fchk_cell.readlines():
data = data.strip()
if not data:
continue
if data.startswith('check_name'):
data = data.split(":")
check_name = data[1].strip()
continue
elif data.startswith('End') or '-' in data:
check_name = ""
not_run_flag = False
run_flag = False
continue
elif data.startswith('not run'):
chk_cell_data[check_name]['not_run'] = []
not_run_flag = True
continue
elif data.startswith('run'):
chk_cell_data[check_name]['run'] = []
not_run_flag = False
run_flag = True
continue
if not_run_flag:
chk_cell_data[check_name]['not_run'].append(data)
elif run_flag:
chk_cell_data[check_name]['run'].append(data)
print(chk_cell_data)
if __name__ == "__main__":
main()