I am trying to parse a text file that has data like below:
============================= condition 1 ============================ condition 2 string col 1 col2 tags ------------------------------ xx xx abc xx xx ac col4 col 1 col5 col6 col7 col8 col9 col10 col11 col12 col13 ----------------------------------------------------------------------------------------------------- 1 11 6 30 abc text -2794 682 57388 294 210 2 11 6 30 ac text -2447 680 52973 302 214 3 11 13 text -2619 -805 120956 568 255 4 11 16 text 2185 -1261 116983 548 273 5 11 17 text -3362 -1413 127136 569 278 Criterion 30 : xxxxx text3 11 : some text here ============================================================================
Below are the things that I want to do
- look for condition 1 and if that is met, make sure condition 2 string is present
- pick the values in the column 'tags'
- and then look for these tags in the table below to extract information from columns 9 to 13.
I can do the third part however, I am struggling with the first two as when I use f.next() to check the condition 2 it is messing up my code :
with open(each_file) as f:
copy = False
i = 0
for linenum,line in enumerate(f):
if line.strip() == "============================= Condition 1 ============================":
line_next = f.next()
if line_next.strip() == "condition 2 string":
print "here1"
print line.strip()
copy = True ## Start copying when both the conditions are met
elif line_next.strip() == "col4 col 1 col5 col6 col7 col8 col9 col10 col11 col12 col13": ## Stop copying at this line
if i == 0:
copy = False
else:
copy = False
i = i + 1
elif copy:
print copy
print line
Please help me with this.