简体   繁体   English

在 ruamel.yaml 中保留列表

[英]Preserving lists in ruamel.yaml

I have a yaml file with some value as a list on multiple lines:我有一个 yaml 文件,其中包含一些值作为多行列表:

my_list: [1,2,3,4,5,6,7,8,9,10,
          11,12,13,14,15,16,17,18,19,20]

If I load and then dump this using ruamel.yaml it does two things:如果我使用 ruamel.yaml 加载然后转储它,它会做两件事:

  1. it puts the list in one line它将列表放在一行中
  2. it adds a space between items它在项目之间添加了一个空间
my_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

is there a way to leave a list alone using ruamel.yaml?有没有办法使用 ruamel.yaml 单独留下一个列表?

I have tried looking for documentation on ruamel.yaml but I can't find too much about this.我曾尝试查找有关 ruamel.yaml 的文档,但我找不到太多相关信息。 I know that there is LiteralScalarString which helps for strings, but I sort of need an equivalent for lists.我知道 LiteralScalarString 对字符串有帮助,但我需要一个等价的列表。

As it says in the introduction to the documentation正如文档介绍中所说

block style and key ordering are kept, so you can diff the round-tripped source保留块样式和键顺序,因此您可以区分往返源

It will not preserve breaks (or comments) inserted within flow-style sequence elements (or flow-style mapping key-values).它不会保留在流式序列元素(或流式映射键值)中插入的中断(或注释)。 All such flow sequences are normalised to have a space before the scalar if preceded by a flow-sequence element indicator ( , ).如果前面有流序列元素指示符 ( , ),则所有此类流序列都被规范化为在标量之前有一个空格。 There is no easy fix of searching for the string ', ' and replacing it with ',' because of that.因此,搜索字符串', '并将其替换为','并不容易。

The LiteralScalarString doesn't do much special, execpt keeping line breaks during dumping. LiteralScalarString没有做太多特别的事情,只是在转储期间保持换行符。 The FoldedScalarString does something more complex in preserving the positions where the original string was folded while allowing you to use the string as if normally loaded. FoldedScalarString在保留原始字符串折叠的位置同时允许您像正常加载一样使用字符串方面做了一些更复杂的事情。 To preserve the "folds" in a flow style sequence, you could use such a mechanism.要在流式序列中保留“折叠”,您可以使用这样的机制。 However since superfluous newlines in block-style are kept as (empty) comments it is more likely that ruamel.yaml will at some point support intra flow-style collection comments (indexed to an element, key or value) and that would give you your newline in a different way.但是,由于块样式中多余的换行符保留为(空)注释,因此 ruamel.yaml 更有可能在某个时候支持内部流式集合注释(索引到元素、键或值),这会给你你的以不同的方式换行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM