使用下面描述的代碼,我可以成功檢索存儲到file.cfg中的屬性,但是如何將輸出用於其他變量?從ConfigParse返回可用值
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
接受您的答案在7分鐘內。感謝您的直接答覆。 –