简体   繁体   English

ConfigParser和具有不带键的值的部分

[英]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? 有没有机会在* .ini文件中创建节以仅存储没有键的值? I'm to store list of used ports in localhost and other servers and my list looks like this: 我要将已用端口的列表存储在localhost和其他服务器中,列表如下所示:

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

For now python treats server name as a key and port as value. 目前,python将服务器名称视为键,将端口视为值。 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? 知道怎么做对吗?

You have the option allow_no_value, but you can not avoid ":" being a value separator, this is at ConfigParser.py: 您可以使用allow_no_value选项,但不能避免将“:”作为值分隔符,它位于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/tab
    r'(?P<value>.*)$'                     # everything up to eol
    )

The only solution that comes to my mind: 我想到的唯一解决方案是:

[servers]

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

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

在我看来,最好使用xml而不是ini ...这是您的替代选择吗?

I don't think you can make ConfigParser treat colons as anything but key/value delimiters. 我认为您不能使ConfigParser将冒号视为除键/值定界符之外的任何内容。 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: ConfigParser支持没有值的键:

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. 另一种选择是向每行添加唯一的ID,并用冒号分隔。 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')]

If you can, please change the format like this: 如果可以的话,请像这样更改格式:

[servers]
localhost:1111,2222,3333
someserver: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(','). 读取时,将密钥读取为服务器名称,然后将文件中的值转换为通过value.split(',')从字符串中的列表。 It will be easy for you to check for the port. 您可以轻松检查端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM