现有conf文件,类型是ini类型的,其中有个bool类型的变量,想要获得对应的值:
<code>[MONGO] ;PORT=27017 PORT=32018 ;USE_AUTH = True USE_AUTH = False </code>
python ini config bool
13.2. ConfigParser — Configuration file parser — Python 2.7.15 documentation
14.2. configparser — Configuration file parser — Python 3.6.5 documentation
ConfigParserExamples – Python Wiki
python – Booleans in ConfigParser always return True – Stack Overflow
最后用代码:
<code>def getConfigParser(): """return project main config's configParser object""" configParser = ConfigParser() projectRootPath = getProjectRootPath() mainConfFileFullPath = os.path.join(projectRootPath, "conf/main.conf") print("mainConfFileFullPath=%s" % mainConfFileFullPath) #'/Users/crifan/dev/dev_root/company/naturling/projects/NLP/sourcecode/naturling/conf/main.conf' configParser.read(mainConfFileFullPath) print("configParser=%s" % configParser) return configParser configParser = getConfigParser() configSection = "MONGO" isUseAuth = configParser.getboolean(configSection, "USE_AUTH") print("isUseAuth=%s" % isUseAuth) </code>
去加上配置:
conf/main.conf
<code>[MONGO] ;USE_AUTH = True ;USE_AUTH = False ;USE_AUTH = yes ;USE_AUTH = no ;USE_AUTH = 1 USE_AUTH = 0 </code>
实现了要的效果:
True/False
yes/no
1/0
均可以被识别为bool的True/False
转载请注明:在路上 » 【已解决】Python中如何从ini类型配置中获取布尔类型配置的值