alp*_*ric 13 python config key configparser
使用ConfigParser的has_section()方法,我可以检查文件中是否存在某个部分,例如:
config.has_section(section_name)
Run Code Online (Sandbox Code Playgroud)
什么是检查密钥是否存在的命令?因此,在使用以下方法查询值之前,可以验证段和键是否存在:
value = config.get(section, key)
Run Code Online (Sandbox Code Playgroud)
提前致谢!
Joh*_*024 21
除此之外has_section,还有一种has_option方法:
config.has_option(section, option)
Run Code Online (Sandbox Code Playgroud)
从Python文档:
has_option*(section,option)*
如果给定的部分存在,并且包含给定的选项,则返回True; 否则返回False.如果指定的部分为None或空字符串,则假定为DEFAULT.