I have a keywords.txt file like this:
#section1
keyword1
keyword2
......
#section2
keyword3
keyword4
......
#section3
keyword5
keyword6
......
there are many keywords in each sections and there are many secitions. My question is : How to extract each section into separated list as following output:
section1=["keyword1","keyword2"]
section2=["keyword3","keyword4"]
......
This is what I have done, to extract the line number of the separator "#"
separator_numlist=[]
with open("keywords.txt") as f:
for num,line in enumerate(f):
if('#') in line:
separator_numlist.append()
"""
Then read lines between each separator's line number
"""
Is there a better solution? Also I'm thinking to store these keywords in XML or json, perhaps reading sections from structured files are more efficiency than reading from txt file.