Consider the following data structure:
[HEADER1]
{
key value
key value
...
[HEADER2]
{
key value
...
}
key value
[HEADER3]
{
key value
[HEADER4]
{
key value
...
}
}
key value
}
There are no indents in the raw data, but I added them here for clarity. The number of key-value pairs is unknown, '...' indicates there could be many more within each [HEADER] block. Also the amount of [HEADER] blocks is unknown.
Note that the structure is nested, so in this example header 2 and 3 are inside header 1 and header 4 is inside header 3.
There can be many more (nested) headers, but I kept the example short.
How do I go about parsing this into a nested dictionary structure? Each [HEADER] should be the key to whatever follows inside the curly brackets.
The final result should be something like:
dict = {'HEADER1': 'contents of 1'}
contents of 1 = {'key': 'value', 'key': 'value', 'HEADER2': 'contents of 2', etc}
I'm guessing I need some sort of recursive function, but I am pretty new to Python and have no idea where to start.
For starters, I can pull out all the [HEADER] keys as follows:
path = 'mydatafile.txt'
keys = []
with open (path, 'rt') as file:
for line in file:
if line.startswith('['):
keys.append(line.rstrip('\n'))
for key in keys:
print(key)
But then what, maybe this not even needed?
Any suggestions?