2014-01-11 42 views
3

使用ConfigParser的has_section()方法,如果該部分在文件中都存在,我可以檢查:的Python ConfigParser科大價值

config.has_section(section_name) 

我不知道什麼是檢查是否關鍵存在的命令好。 因此,這將有可能使用驗證的queering值之前,這兩個節和項是否存在:提前

value = config.get(section, key) 

謝謝!

+0

這可能會幫助你http://stackoverflow.com/questions/287757/pythons-configparser-unique-keys-per-section – samrap

回答

4

除了has_section,也有一個has_option方法:

config.has_option(section, option) 

Python documentation

has_option *(部分,選項)*
如果給定的部分存在,並且包含 給定的選項,返回True;否則返回False。如果 指定節是None或空字符串,則假定爲DEFAULT。