Lists in ConfigParser

后端 未结 15 2025
清酒与你
清酒与你 2020-11-27 09:38

The typical ConfigParser generated file looks like:

[Section]
bar=foo
[Section 2]
bar2= baz

Now, is there a way to index lists like, for in

15条回答
  •  心在旅途
    2020-11-27 10:02

    I completed similar task in my project with section with keys without values:

    import configparser
    
    # allow_no_value param says that no value keys are ok
    config = configparser.ConfigParser(allow_no_value=True)
    
    # overwrite optionxform method for overriding default behaviour (I didn't want lowercased keys)
    config.optionxform = lambda optionstr: optionstr
    
    config.read('./app.config')
    
    features = list(config['FEATURES'].keys())
    
    print(features)
    

    Output:

    ['BIOtag', 'TextPosition', 'IsNoun', 'IsNomn']
    

    app.config:

    [FEATURES]
    BIOtag
    TextPosition
    IsNoun
    IsNomn
    

提交回复
热议问题