Python ConfigParser检查Section和Key Value的存在

使用ConfigParser的has_section()方法,我可以检查文件中是否存在某个部分,例如:

config.has_section(section_name)

什么是检查密钥是否存在的命令?
因此,在使用以下方法查询值之前,可以验证段和键是否存在:

value = config.get(section,key)

提前致谢!

解决方法

除了has_section之外,还有一个has_option方法:

config.has_option(section,option)

从Python documentation:

has_option*(section,option)* If the given section exists,and contains the given option,return True; otherwise return False. If the specified section is None or an empty string,DEFAULT is assumed.

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章