ConfigParser和多个选项名称 [英] ConfigParser and multiple option names

查看:61
本文介绍了ConfigParser和多个选项名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

因为ConfigParser似乎不支持多次相同的选项

名称,如:


dir =" / home / florian"

dir =" / home / john"

dir =" / home / whoever"


(只读最后一个)


我想知道解决这个问题的最佳方法是什么。


我想最好的解决方案是使用分离字符:


dir =" / home / florian,/ home / john,home / whoever"


哪种角色最适合各种操作系统? (什么

名称可能是路径包含的问题)


您怎么看?还有更好的想法吗?


谢谢,


Florian

解决方案

< blockquote> FlorianLindnerescribió:

我认为最好的解决方案是使用分离字符:

dir =" / home / florian,/ home / john,home /谁"


RCS在文件名中使用

您怎么看?有什么更好的想法吗?




有点难看,但可能比添加任意分隔符更安全,更简单:


[section]

dir_1 = / home / florian

dir_2 = / home / john

dir_3 = / home / whoever

as( a | i)足够的实现给你的想法,它有一些错误:


import ConfigParser

import re


class ConfigParserWithLists(ConfigParser.ConfigParser):

def setlist(self,section,option,value):

for i,v in enumerate(value):

self.set(section,''%s_%i''%(选项,i + 1),v)


def getlist(self,section ,选项):

res = []

m = re.compile(''^''+ option +''_\d +

'')。匹配

for self.options(部分):

如果m(oo):

res .append(self.get(section,oo))

返回res


HTH

Alexis Roda写道:

Florian Lindner escribi?3:

我认为最好的解决方案是使用分离字符:

dir =" / home / florian,/ home / john,home / whoever"
RCS在文件名中使用




A kommata(, )是路径名中的有效字符。好的,你可以使用引号。

你怎么看?有什么更好的想法吗?



有点难看,但可能比添加任意分隔符更安全,更简单:

[section]
dir_1 = / home / florian
dir_2 = / home / john
dir_3 = / home / whoever




我倾向于使用分隔符,因为我认为这对用户来说更为常见。 (

PATH环境变量例如)

as(a | i)实现给你的想法,它有一些错误:



[...]


感谢您的投入!


Florian


Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

(only the last one is read in)

I wonder what the best way to work around this.

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of what
names may a path consist is the question)

What do you think? Any better ideas?

Thanks,

Florian

解决方案

Florian Lindner escribió:

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"
RCS uses , in filenames
What do you think? Any better ideas?



A bit ugly, but probably safer and simpler than adding arbitrary separators:

[section]
dir_1=/home/florian
dir_2=/home/john
dir_3=/home/whoever
a s(a|i)mple implementation to give you the idea, it has some bugs:

import ConfigParser
import re

class ConfigParserWithLists(ConfigParser.ConfigParser):
def setlist(self, section, option, value) :
for i, v in enumerate(value) :
self.set(section, ''%s_%i'' % (option, i + 1), v)

def getlist(self, section, option) :
res = []
m = re.compile(''^'' + option + ''_\d+


'').match
for oo in self.options(section) :
if m(oo) :
res.append(self.get(section, oo))
return res


HTH


Alexis Roda wrote:

Florian Lindner escribi?3:

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"
RCS uses , in filenames



A kommata (,) is a valid character in path names. Ok, you can use quotes.

What do you think? Any better ideas?



A bit ugly, but probably safer and simpler than adding arbitrary
separators:

[section]
dir_1=/home/florian
dir_2=/home/john
dir_3=/home/whoever



I tend to use seperators, because I think it''s more common to users. (the
PATH environment variable e.g.)
a s(a|i)mple implementation to give you the idea, it has some bugs:


[...]

Thanks for your input!

Florian


这篇关于ConfigParser和多个选项名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆