Python ConfigParser检查Section和Key Value的存在

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/python/186032.html

猜你在找的Python相关文章