2011-12-20 87 views
27

我想用配置解析器Python配置解析器獲取節中的所有值?

我用這個來從一節中的所有值,但它僅給出了第一個值

def ConfigSectionMap(section): 
    dict1 = {} 
    options = Config.options(section) 
    for option in options: 
    try: 
     dict1[option] = Config.get(section, option) 
     if dict1[option] == -1: 
     DebugPrint("skip: %s" % option) 
    except: 
     print("exception on %s!" % option) 
     dict1[option] = None 
    return dict1 


    Config = ConfigParser.ConfigParser() 
    Config.read("/etc/harvest.conf") 
    print ConfigSectionMap("files").values() 
+2

您的'返回'不正確nted,你的函數在for循環的第一次迭代中返回。刪除兩個空格。 – Chewie 2011-12-20 16:27:02

回答

75

使它成爲一個字典:

dict(Config.items('Section')) 
1

你可以如果訂購非常重要,請將其列爲清單

list(Config.items('Section'))