首页 > 解决方案 > 从 Python(2.7) 中的配置文件中读取该部分作为字典

问题描述

我有一个包含以下部分的配置文件


[TEST]

fruit=["apple","select * from apple"]
vegetable=["carrot","select * from carrot"]

我正在尝试使用以下代码将该部分加载到 python 字典中

   config=ConfigParser.RawConfigParser()
   config.optionxform =str
   cofigPath='/home/ini/target.ini'
   config.read(configPath)

现在,当我试图将该部分加载到字典中时,我没有在值中获取列表,而是将其存储为字符串

   tabList=dict(config.items('TEST'))
   for k, v in tableList.iteritems():
       print(k,v[0])

v[0] 没有打印 "apple" ,而是将字典的值部分作为字符串并将 v[0] 打印为 "[" ,即字典值的第一个字符。

请建议我应该合并什么以将该部分读入字典,当我遍历字典时我可以将列表作为值

标签: pythonpython-2.7

解决方案


推荐阅读