當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python configparser.ConfigParser.SECTCRE用法及代碼示例

用法:

ConfigParser.SECTCRE

用於解析節標題的編譯正則表達式。默認匹配 [section] 到名稱 "section" 。空格被認為是部分名稱的一部分,因此 [  larch  ] 將被讀取為名稱 "  larch  " 的部分。如果不合適,請覆蓋此屬性。例如:

>>> import re
>>> config = """
... [Section 1]
... option = value
...
... [  Section 2  ]
... another = val
... """
>>> typical = configparser.ConfigParser()
>>> typical.read_string(config)
>>> typical.sections()
['Section 1', '  Section 2  ']
>>> custom = configparser.ConfigParser()
>>> custom.SECTCRE = re.compile(r"\[ *(?P<header>[^]]+?) *\]")
>>> custom.read_string(config)
>>> custom.sections()
['Section 1', 'Section 2']

注意

雖然 ConfigParser 對象也使用 OPTCRE 屬性來識別選項行,但不建議覆蓋它,因為這會幹擾構造函數選項 allow_no_valuedelimiters

相關用法


注:本文由純淨天空篩選整理自python.org大神的英文原創作品 configparser.ConfigParser.SECTCRE。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。