我尝试使用 Config.ini 文件中的部分详细信息打印一个统一的Python字典,但不幸的是我得到了单独的列表或只有最终的字典。
我很想得到你们的帮助。
config_path = r"<Path>\Config.ini"
dict = {}
config = ConfigParser.ConfigParser()
config.read(config_path)
sections = config.sections()
for section in sections:
sql_query = config.get(section, 'SQL')
limit = config.get(section, 'Limit')
description = config.get(section, 'Description')
section_name = section
dict = {'Query': sql_query,
'Limit': limit,
'Description': description,
'Section': section_name}
print dict
答案 0 :(得分:0)
您在每个循环中覆盖您的字典。
config = ConfigParser.ConfigParser()
config.read(config_path)
sections = config.sections()
config_sections = []
for section in sections:
config_sections.append(
{'Query': config.get(section, 'SQL'),
'Limit': config.get(section, 'Limit'),
'Description': config.get(section, 'Description'),
'Section': section})
print config_sections
config = ConfigParser.ConfigParser()
config.read(config_path)
sections = config.sections()
config_dict = {}
for section in sections:
for name, value in [('Query', config.get(section, 'SQL')),
('Limit', config.get(section, 'Limit')),
('Description', config.get(section, 'Description')),
('Section', [section])]:
config_dict.setdefault(name, []).append(value)
print config_dict