给定以下格式(.properties 或 .ini):
propertyName1=propertyValue1
propertyName2=propertyValue2
...
propertyNameN=propertyValueN
对于 Java 有 Properties提供解析/与上述格式交互的功能的类。
python 的 standard 库 (2.x) 中是否有类似的东西?
如果没有,我还有什么其他选择?
最佳答案
我能够让它与 ConfigParser
一起工作,没有人展示任何关于如何做到这一点的示例,所以这里是一个简单的属性文件的 python 阅读器和属性文件的示例。请注意,扩展名仍然是 .properties
,但我必须添加一个类似于您在 .ini 文件中看到的部分标题...有点 SCSS ,但它可以工作。
python 文件:PythonPropertyReader.py
#!/usr/bin/python
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('ConfigFile.properties')
print config.get('DatabaseSection', 'database.dbname');
属性文件:ConfigFile.properties
[DatabaseSection]
database.dbname=unitTest
database.user=root
database.password=
有关更多功能,请阅读:https://docs.python.org/2/library/configparser.html
关于python - python中的属性文件(类似于Java Properties),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3595363/