繁体   English   中英

python configparser将部分值映射到其他部分

[英]python configparser map section values to other sections

我希望能够从配置文件部分读取文件的可变列表,并使用该列表中的键来指向定义每个文件属性的其他部分。 文件可以更改数量,名称和属性。 有没有人看到任何这样做的方法,或者可以指出正确的方向?

[paths]
file1=/some/path/
file2=/some/other/path

[file1]
key_specific_to_file_1=some_attribute_value

[file2]
key_specific_to_file_2=some_attribute_value2

[non-file-related-section]
some_key=some-other-value

有标准模块configparser。 可以找到configparser文档

简单的例子:



    #/usr/bin/env python
    import configparser
    import sys

    def config_reader(filename):
        try:
            config = configparser.ConfigParser()
            config.read(filename )
            section_list = config.sections()
            for section_name in section_list:
                for key in config[section_name]:
                    print("Key : " +  config[section_name ][key])
        except configparser.Error as e:
            print(e)
            return 

    def main():
        print("config file " + sys.argv[1])
        config_reader(sys.argv[1])


    if __name__ == "__main__":
        main()


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM