0

I am trying to write a program to parse a file, break it into sections, and read it into a nested dictionary. I want the output to be something like this:

output = {'section1':{'nested_section1':{'value1':'value2'}}}

I'm trying to do this by building separate dictionaries, than merging them, but I'm running into trouble naming them. I want the dictionaries inside of the others to be named based on the sections of the file they're taken from. But it seems I can't name a dictionary from a variable.

KJWing
  • 13
  • 1
  • 5
  • Not sure what you are asking, but if you got `name = 'section1'`, then `{name: {'nested_section1':{'value1':'value2'}}}` will result in the same value as `output`. – metatoaster Aug 07 '14 at 23:39
  • 1
    Very difficult to gather what you are asking for w/o the code and sample input – shaktimaan Aug 07 '14 at 23:40
  • Yes please post an example piece of code that's throwing an error so we can see what you're trying to do. – anderspitman Aug 08 '14 at 00:51
  • The only variable name shown in your example is `output`, yet the dictionary associated with it doesn't contain the string `"output"` -- so your question makes little sense as it stands. – martineau Aug 08 '14 at 01:25

2 Answers2

0

You can name a dictionary entry from a variable. If you have

text = "myKey" # or myNumber or any hashable type
data = dict()

You can do

data[text] = anyValue
Steve K
  • 10,879
  • 4
  • 39
  • 39
0

Store all your dictionaries in a single root dictionary.

all_dicts['output'] = {'section1':{'nested_section1':{'value1':'value2'}}}

As you merge dictionaries, remove the children from all_dicts.

all_dicts['someotherdict']['key'] = all_dicts['output']
del all_dicts['output']
chepner
  • 497,756
  • 71
  • 530
  • 681