Python配置解析器从一个节中获取所有值?
我想使用配置解析器从一个节中获取所有值 我使用了这个,但它只给出了第一个值Python配置解析器从一个节中获取所有值?,python,parsing,configparser,Python,Parsing,Configparser,我想使用配置解析器从一个节中获取所有值 我使用了这个,但它只给出了第一个值 def ConfigSectionMap(section): dict1 = {} options = Config.options(section) for option in options: try: dict1[option] = Config.get(section, option) if dict1[option] == -1: DebugPrint
def ConfigSectionMap(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
Config = ConfigParser.ConfigParser()
Config.read("/etc/harvest.conf")
print ConfigSectionMap("files").values()
让它成为一句格言:
dict(Config.items('Section'))
如果订购很重要,你可以把它列成一个清单
list(Config.items('Section'))
您的
返回
没有正确缩进,并且您的函数在for循环的第一次迭代中返回。删除两个空格。不适用于我(使用dict的版本适用)。我发现:**参数中有错误:“(Config.items('Section'))”。