ConfigObj/ConfigParser vs. using YAML for Python settings file(ConfigObj/ConfigParser 与为 Python 设置文件使用 YAML)
问题描述
为 Python 程序创建设置文件,内置模块(ConfigParser)或独立项目(ConfigObj),或使用 YAML 数据序列化格式,哪个更好?我听说 ConfigObj 比 ConfigParser 更容易使用,尽管它不是内置库.我还读到 PyYAML 很容易使用,尽管 YAML 需要一些时间来使用.除了易于实施之外,哪个是创建设置/配置文件的最佳选择?
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?
推荐答案
使用 ConfigObj 至少非常简单,general 中的 ini 文件比 YAML 简单得多(并且使用更广泛).对于更复杂的情况,包括验证、默认值和类型,ConfigObj 提供了一种通过 configspec 验证来执行此操作的方法.
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.
使用 ConfigObj 读取 ini 文件的简单代码:
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
这篇关于ConfigObj/ConfigParser 与为 Python 设置文件使用 YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ConfigObj/ConfigParser 与为 Python 设置文件使用 YAML
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
