Skip to content Skip to sidebar Skip to footer

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 lis

Solution 1:

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']

Solution 2:

You have the option allow_no_value, but you can not avoid ":" being a value separator, this is at ConfigParser.py:

OPTCRE = re.compile(
    r'(?P<option>[^:=\s][^:=]*)'# very permissive!r'\s*(?P<vi>[:=])\s*'# any number of space/tab,# followed by separator# (either : or =), followed# by any # space/tabr'(?P<value>.*)$'# everything up to eol
    )

The only solution that comes to my mind:

[servers]s1 = localhost:1111s2 = localhost:2222s3 = localhost:3333s4 = someserver:2222s5 = someserver:3333

Solution 3:

In my opinion it would be better to use an xml rather than an ini ... is this an alternative to you?

Solution 4:

I don't think you can make ConfigParser treat colons as anything but key/value delimiters. Thus, if you use colons, the hostnames will be interpreted as keys, which won't work for you because they are not unique. So you will probably have to change colons to something else. Then your entries will be unique. ConfigParser supports keys without values:

In [1]: from ConfigParser import ConfigParser

In [2]: cp = ConfigParser(allow_no_value=True)

In [3]: cp.read('foo.conf')
Out[3]: ['foo.conf']

In [4]: cp.items('servers')
Out[4]: 
[('localhost;1111', None),
 ('localhost;2222', None),
 ('localhost;3333', None),
 ('someserver;2222', None),
 ('someserver;3333', None)]

Another option is to add a unique ID to each line and also separate it by a colon. The rest will then become the value:

In [1]: from ConfigParser import ConfigParser

In [2]: cp = ConfigParser()

In [3]: cp.read('foo.conf')
Out[3]: ['foo.conf']

In [4]: cp.items('servers')
Out[4]: 
[('1', 'localhost:1111'),
 ('2', 'localhost:2222'),
 ('3', 'localhost:3333'),
 ('4', 'someserver:2222'),
 ('5', 'someserver:3333')]

Solution 5:

If you can, please change the format like this:

[servers]
localhost:1111,2222,3333someserver:4444,5555,6666

While you read, read the key as server name and convert the value from the file as list from string through value.split(','). It will be easy for you to check for the port.

Post a Comment for "Configparser And Section With Values Without Keys"