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?
If you can, please change the format like this:
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.
In my opinion it would be better to use an xml rather than an ini ... is this an alternative to you?
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: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:
You have the option allow_no_value, but you can not avoid ":" being a value separator, this is at ConfigParser.py:
The only solution that comes to my mind:
You could store the servers in a comma separated list,
the read it into a list like
which outputs