Python属性文件

alx*_*xbx 1 python properties properties-file

我想在Python中制作一些属性文件,就像在Java中一样(application.properties,application.yaml)。

Python项目中有什么解决方案吗?我也不想使用某些属性解析器,如果某些东西像Java中那样开箱即用,那就太好了。

de1*_*de1 5

等效的Python是读取INI文件的configparser:https ://docs.python.org/3/library/configparser.html

它与属性文件相似,但不相同。

示例INI文件example.ini(从链接的文档中复制):

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no
Run Code Online (Sandbox Code Playgroud)

和一个代码示例(也从文档中复制):

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
Run Code Online (Sandbox Code Playgroud)