Properties file in python (similar to Java Properties)

后端 未结 25 2873
故里飘歌
故里飘歌 2020-11-29 17:41

Given the following format (.properties or .ini):

propertyName1=propertyValue1
propertyName2=propertyValue2
...
propertyNam         


        
25条回答
  •  广开言路
    2020-11-29 18:47

    I was able to get this to work with ConfigParser, no one showed any examples on how to do this, so here is a simple python reader of a property file and example of the property file. Note that the extension is still .properties, but I had to add a section header similar to what you see in .ini files... a bit of a bastardization, but it works.

    The python file: PythonPropertyReader.py

    #!/usr/bin/python    
    import ConfigParser
    config = ConfigParser.RawConfigParser()
    config.read('ConfigFile.properties')
    
    print config.get('DatabaseSection', 'database.dbname');
    

    The property file: ConfigFile.properties

    [DatabaseSection]
    database.dbname=unitTest
    database.user=root
    database.password=
    

    For more functionality, read: https://docs.python.org/2/library/configparser.html

提交回复
热议问题