Python ConfigParser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • derf912
    New Member
    • May 2007
    • 2

    Python ConfigParser

    I am working with ConfigParser in python and the ini file that I am reading in has a section titled [ClientList] and under that section I have multiple options. All the options have the same name "customer ="
    Eample:

    [ClientList]
    customer = widget1
    customer = widget2
    customer = widget3
    customer = widget4

    I am trying to get a list of all the options but have only been able to get the last one to print. Here is the script that I am working with right now and the only thing it provides me is the last option in my list. Is this even possible with ConfigParser and if so can someone help me?

    Thanks


    Code:
    # Import Statment 
    import ConfigParser 
    import os 
    
    # Script Variables 
    inifile = 'client.ini.txt' 
    client = {}
    
    
    # Reading in config file 
    config = ConfigParser.ConfigParser() 
    config.read(inifile) 
    
    
    def main():
        x = 0
        for section in config.sections(): 
            if section == "ClientList": 
                for option in config.options(section):
                    client[x] = config.get(section, option)
                    #print " ", option, "=", config.get(section, option)
                    print client
                    x = x+1
    
    if __name__=="__main__": 
    
        main()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by derf912
    I am working with ConfigParser in python and the ini file that I am reading in has a section titled [ClientList] and under that section I have multiple options. All the options have the same name "customer ="
    Eample:

    [ClientList]
    customer = widget1
    customer = widget2
    customer = widget3
    customer = widget4

    I am trying to get a list of all the options but have only been able to get the last one to print. Here is the script that I am working with right now and the only thing it provides me is the last option in my list. Is this even possible with ConfigParser and if so can someone help me?

    Thanks


    Code:
    # Import Statment 
    import ConfigParser 
    import os 
    
    # Script Variables 
    inifile = 'client.ini.txt' 
    client = {}
    
    
    # Reading in config file 
    config = ConfigParser.ConfigParser() 
    config.read(inifile) 
    
    
    def main():
        x = 0
        for section in config.sections(): 
            if section == "ClientList": 
                for option in config.options(section):
                    client[x] = config.get(section, option)
                    #print " ", option, "=", config.get(section, option)
                    print client
                    x = x+1
    
    if __name__=="__main__": 
    
        main()
    'INI' files generally have only unique variable names. Therefore it's no surprise that only the last customer is returned. Do something like this to get your data:[code=Python]f = open(fn)
    for line in f:
    outList = []
    while '[ClientList]' not in line:
    line = f.next()
    line = f.next()
    while not line.startswith ('['):
    outList.append( line.strip())
    line = f.next()
    break
    f.close()
    print outList[/code]>>> ['customer = widget1', 'customer = widget2', 'customer = widget3', 'customer = widget4', '']

    Comment

    • derf912
      New Member
      • May 2007
      • 2

      #3
      That works!
      So ConfigParser will only read options with unique name then?

      Thanks

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by derf912
        That works!
        So ConfigParser will only read options with unique name then?

        Thanks
        You are welcome. This works:[code=Python]p = ConfigParser()
        p.readfp(file_o bject)
        print p.items('Client List')[/code]
        >>> [('customer1', 'widget1'), ('customer3', 'widget3'), ('customer2', 'widget2'), ('customer4', 'widget4')]

        Unique names are required. The instance data is maintained as a nested dictionary.[code=Python]>>> p.__dict__['_sections']['ClientList']
        {'customer1': 'widget1', '__name__': 'ClientList', 'customer3': 'widget3', 'customer2': 'widget2', 'customer4': 'widget4'}
        >>>[/code]

        Comment

        Working...