问题
Which is better for creating a settings file for Python programs, the built-in module (ConfigParser) or the independent project (ConfigObj), or using the YAML data serialization format? I have heard that ConfigObj is easier to use than ConfigParser, even though it is not a built-in library. I have also read that PyYAML is easy to use, though YAML takes a bit of time to use. Ease of implementation aside, which is the best option for creating a settings/configuration file?
回答1:
Using ConfigObj is at least very straightforward and ini files in general are much simpler (and more widely used) than YAML. For more complex cases, including validation, default values and types, ConfigObj provides a way to do this through configspec validation.
Simple code to read an ini file with ConfigObj:
from configobj import ConfigObj
conf = ConfigObj('filename.ini')
section = conf['section']
value = section['value']
It automatically handles list values for you and allows multiline values. If you modify the config file you can write it back out whilst preserving order and comments.
See these articles for more info, including examples of the type validation:
- http://www.voidspace.org.uk/python/articles/configobj.shtml
- http://www.blog.pythonlibrary.org/2010/01/01/a-brief-configobj-tutorial/
- http://showmedo.com/videotutorials/video?name=10620000&fromSeriesID=1062
回答2:
It depends on what you want to store in your config files and how you use them
If you do round tripping (yaml→code→yaml) and want comments preserved you cannot use
PyYAML
orConfigParser
.If you want to preserve the order of your keys (e.g. when you check in your config files),
PyYAML
doesn't do that unless you specify!!omap
(which makes it less easy to update than a normal mapping)If you want to have complex structures with lists of unnamed elements containing mappings/dictionaires, then
ConfigParser
andConfigObj
won't help you as the INI files key-value pairs have to go into sections and lists can only be values.
The ruamel.yaml implementation of the YAML reader supports all of the above ¹. I have used fuzzyman's excellent ConfigObj for round trip comment preservation for a long time, as well as PyYAML for more complex structures and this combines best of both worlds. ruamel.yaml
includes the yaml
utility that can convert ConfigObj
INI files to YAML
¹ ruamel.yaml is a YAML library that supports YAML 1.2 (I recommend using that, but then I am the author of the package). PyYAML only supports (most of) YAML 1.1.
回答3:
ConfigParser has a really bad API, ConfigObj is supposed to be good but I have never used it, for ini files I usually implement my own parser.
However ini-like formats don't handle different types, sequences or recursive mappings well anyway so I would just use yaml. It's easy to read and edit with an editor, there is a standard for parsing those files and you have none of the mentioned downsides ini-like formats have.
来源:https://stackoverflow.com/questions/3444436/configobj-configparser-vs-using-yaml-for-python-settings-file