Search code examples
pythonconfigparser

ConfigParser and section with values without keys


I'm just wondering. Is there any chance to create section in *.ini file to store only values without keys? I'm to store list of used ports in localhost and other servers and my list looks like this:

[servers]
localhost:1111
localhost:2222
localhost:3333
someserver:2222
someserver:3333

For now python treats server name as a key and port as value. But worst thing is that calling

print config.items('servers')

Returns me only this:

localhost:3333
someserver:3333

which is wrong, but I could handle it by replacing : in config but still section needs key for values. Any idea how to do it right?


Solution

  • You could store the servers in a comma separated list,

    [servers] 
    server_list = localhost:1111, localhost:2222, localhost:3333, someserver:2222, someserver:3333
    

    the read it into a list like

    from ConfigParser import ConfigParser
    
    cp = ConfigParser()
    cp.read('derp.config')
    print cp.items('servers')[0][1].split(', ')
    

    which outputs

    ['localhost:1111', 'localhost:2222', 'localhost:3333', 'someserver:2222', 'someserver:3333']