在configpar中使用词典理解

2024-03-29 05:48:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的示例中,我可以从[section1]中获取值。我怎样才能为其他部分或更多部分做到这一点?你知道吗

存储配置

[section1]
field_a = hello
field_b = galaxy

[section2]
field_a = hello
field_b = galaxy

[section3]
field_a = hello
field_b = galaxy

主文件.py

from ConfigParser import SafeConfigParser

class Main:

   def get_properties(self, section, *variables):
        cfgFile = 'c:\store.config'
        parser = SafeConfigParser()
        parser.read(cfgFile)
        properties= variables
        return {
            variable: parser.get(section,variable) for variable in properties
        }

   def run_me(self):
        config_vars= self.get_properties('section1','field_a')
        print config_vars

op=Main()
op.run_me()

电流输出:

{'section1': 'field_a'}

这将帮助我改进postUsing var from from function A to function B中给出的解决方案。你知道吗


Tags: fromselfconfigparserfieldhellogetmain
2条回答

解决方案:

def run_me(self):
    config_vars= self.get_properties('services','package_install','package_info')
    convig_vars_2 = self.get_properties('network','proxy_server','proxy_user')

就这么简单。你知道吗

我相信你在寻找你的SafeConfigParser中的the ^{} method。对于您的示例,它应该返回['section1', 'section2', 'section3'],您可以对其进行迭代。你知道吗

相关问题 更多 >