Python ConfigParser检查Section和Key Value的存在

Python ConfigParser检查Section和Key Value的存在,第1张

概述使用ConfigParser的has_section()方法,我可以检查文件中是否存在某个部分,例如: config.has_section(section_name) 什么是检查密钥是否存在的命令? 因此,在使用以下方法查询值之前,可以验证段和键是否存在: value = config.get(section, key) 提前致谢! 除了has_section之外,还有一个has_option方 使用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.

总结

以上是内存溢出为你收集整理的Python ConfigParser检查Section和Key Value的存在全部内容,希望文章能够帮你解决Python ConfigParser检查Section和Key Value的存在所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1207148.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存