配置文件格式,复杂的数据结构(列表,字典),注释,可编辑 [英] Configuration file format, complex datastructures (list, dictionaries), comments, editable

查看:177
本文介绍了配置文件格式,复杂的数据结构(列表,字典),注释,可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我长期以来使用普通的python文件进行配置。我可以添加注释(以提醒我,如果变量名不够用,条目是什么意思),并支持列表和字典。



现在,我需要拥有对于导入客户的产品来说,这似乎有些危险,因为如果他们手工进行错误编辑,可能会破坏系统。



将来,我希望能够从程序中更新文件,并能够使用普通编辑器对其进行编辑(并且希望不必匹配<> ()




  • XML是不合适的,因为它是ASCII伪装形式的二进制文件

  • .ini文件的层次结构很少

  • JSON无法处理注释。



我现在正在考虑向JSON添加注释,但这本质上将需要重写解析器以保留注释。



对于 .ini 文件有一个ConfigObj库,该库保留注释,但 .ini 可以满足我的需求(包含值的字典列表是字典列表)。



还有其他一些配置吗?我应该使用的文件格式?还是应该在导入文件之前以安全的方式解析 .py 文件(这也允许编写文件)

解决方案

正如Chazeon和jme指出的,您应该看看YAML。



YAML还支持嵌套数据结构(列表(YAML中的序列),字典(映射)和各种基元(整数,浮点数,字符串,日期)。



行注释(由引入),但标准 PyYAML Chazeon提到的解析器会在读取数据时扔掉这些东西(并且不能写这些东西)。



ruamel.yaml (我是作者),在进行往返旅行时会保留注释(YAML)文件到python数据结构再转换为YAML文件)。它还保留了大多数的YAML格式(列表和字典使用单行或单行)。



缩进是标准化的,因此所有块映射和序列在第一次往返后看起来都相同。缩进默认为2个空格,但是可以分别设置它们,也可以将t可以在序列元素之前的空格内将破折号推入,例如使用 yaml.indent(mapping = 3,sequence = 5,offset = 2)


I have long used normal python files for configuration purposes. I can include comments (to remind me what an entry was all about if the variable name isn't sufficient) and supports lists and dictionaries.

Now that I need to have something for a customer importing this seems a bit dangerous as it could break the system if they make an error editing by hand.

In the future I want to be able to update the file from within a program, and also be able to edit it with a normal editor ( and prefer not having to match <> or ():

  • XML is inappropriate as it is binary in ascii disguise
  • .ini files have to little hierarchical structure
  • JSON cannot handle comments.

I am now considering adding comments to JSON, but that essentially would require rewriting the parser to preserve comments. And JSON is not that nicely readable.

There is a ConfigObj library for .ini files that preserves comments, but the .ini is to flat for my needs (lists of dicts containing values that are lists of dicts).

Is there some other config file format I should use? Or should I look at parsing my .py files in safe way before importing them (that also allows writing them out again)?

解决方案

As Chazeon and jme indicate you should take a look at YAML. It supports nested data structures ( list (sequence in YAML), dict (mapping) and various primitives ( integers, float, string, date ).

YAML also supports end-of-line comments ( introduced with #) but the "standard" PyYAML parser mentioned by Chazeon throws these away while reading data in (and cannot write these).

The package ruamel.yaml (of which I am the author) that derives from PyYAML, preserves the comments when doing a round trip (YAML file to python datastructures to YAML file). It also keeps most of the YAML formatting (block vs. one-line for lists and dictionaries).

Indentation however is "standardised" so all block mappings and sequences look the same after the first roundtrip. Indentation defaults to 2 spaces for both, but these can be set separately, as well as that the dash can be "pushed in" within the space before the sequence element using e.g. yaml.indent(mapping=3, sequence=5, offset=2)

这篇关于配置文件格式,复杂的数据结构(列表,字典),注释,可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆