加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python ConfigParser检查Section和Key Value的存在

发布时间:2020-12-16 23:27:48 所属栏目:Python 来源:网络整理
导读:使用ConfigParser的has_section()方法,我可以检查文件中是否存在某个部分,例如: config.has_section(section_name) 什么是检查密钥是否存在的命令? 因此,在使用以下方法查询值之前,可以验证段和键是否存在: value = config.get(section,key) 提前致谢!
使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读