Need a little help here. I have a file which is something like below:
category: dog1
member10
member20
category: cat
member1
member2
member3
category: mydog
member100
member200
member300
category: lion
member1000
member2000
member3000
member4000
category: wolf
member4
member5
member6
category: dog4
member400
member500
member600
member700
member800
I am trying to pull the details of all the categries that have "dog" in its name along with its respective members, preferably in a dictionary, so that I can iterate on it for further checks.
Tried many ways like putting up a flag for printing within 'if' loop but still not able to get the correct code.
===========================================
import re
from re import search
keyword = "dog"
should_print = False
file = open("inputfile.txt","r")
lines = file.readlines()
for line in lines:
if (should_print or keyword in line):
print (line.strip("\n"))
should_print = True
===========================================
Above starts with category having "dog" which I want, but then keep printing till the end of file. Not sure where exactly we need to set the 'should_print' flag False and then may be a 'continue' statement.
It would be great if someone can guide or redirect with similar or a different approach.
Thanks in advance.