创建名称为变量的字典 [英] Create a dictionary with name of variable

查看:115
本文介绍了创建名称为变量的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个解析文件的程序,将其分成几部分,并将其读入嵌套字典。我想要输出如下:

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.

推荐答案

存储所有您的字典在单根字典中。

Store all your dictionaries in a single root dictionary.

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

合并词典时,从 all_dicts

all_dicts['someotherdict']['key'] = all_dicts['output']
del all_dicts['output']

这篇关于创建名称为变量的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆