使用下面描述的代码,我可以成功地检索存储到文件.cfg,但如何将输出用于其他变量?你知道吗
from ConfigParser import SafeConfigParser
class Main:
def get_properties(self, section, *variables):
cfgFile = 'c:\file.cfg'
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('database','host','dbname')
print config_vars
op=Main()
op.run_me()
我还在学习Python,但我不确定要如何将输出设置为单个变量:
电流输出:
{'host': 'localhost', 'dbname': 'sample'}
我想要的是:
db_host = localhost
db_name = sample
我建议采用这种方法:
其思想是在一个类中收集所有重要的属性(在这个类中还可以设置默认值)。你知道吗
方法
ConfigProvider.update()
将用配置中的值覆盖这些值(如果它们存在的话)。你知道吗这允许您使用简单的
obj.name
语法访问属性。你知道吗gist
您收到了dict对象
config_vars
,因此可以使用配置变量作为dict的值:阅读documentation中有关python词典的更多信息。你知道吗
相关问题 更多 >
编程相关推荐