Given a File with the contents below :
******************
* Header title 1
* + trig apple
* + targ beans
* + trig grapes
* + targ berries
* Header title 2
* + trig beans
* + targ joke
* + trig help
* + targ me
The above pattern repeats with every header title having a uniq string. As i read the file i would like to create an ordered dict with keys as the Header titles and values as a list of the lines in the body section. So something like this :
d = {
Header title 1: ['+ trig apple', '+ targ beans', '+ trig grapes', '+ targ berries' ],
Header title 2: ['+ trig beans', '+ targ joke', '+ trig grapes', '+ targ berries' ],
.
.
.
<key>: <value>
}
Please i am stuck! My current solution tries to iterate the file line by line to store the values in the list for each header, but i am seeing that it is storing all the body sections for all the headers into the list value for each header. Essentially my solution is not giving what i need.
I indicated above what i tried
The below code will create the file based on your sample input, then read it into an OrderedDict. This assumes headers start with * and records start with * +. It also presupposes that no records occur before the first header is set. You also likely want to clean up your text by removing new lines \n.