简体   繁体   English

无法添加列表成员

[英]Unable to add list member

I'm trying to add a new list member but I'm not being successful. 我正在尝试添加新的列表成员,但没有成功。

This is what I have: 这就是我所拥有的:

import sys
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap as cm
from ruamel.yaml.comments import CommentedSeq as cs

yaml_doc = """\
Condition:
  Like:
    - !Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']]
"""
yaml = YAML()
yaml.preserve_quotes = True
yaml.width = 4096
data = yaml.load(yaml_doc)
ref = data['Condition']['Like']

new_member = "!Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '456', ':tty:*']]"
# new_member2 = '!Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '456', ':tty:*']]'

ref.append(new_member)
# ref.append(new_member2)

yaml.dump(data, sys.stdout)

The output is 输出是

Condition:
  Like:
  - !Join ['', ['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']]
  - "!Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '456', ':tty:*']]"

I do not want the double quotes. 我不要双引号。

If I uncomment the section related to the new_member2 I get and error 如果取消注释与new_member2相关的部分,则会出错

  File "ec2-recover-2.py", line 18
    new_member2 = '!Join ['',['abc: def: ', !Ref 'XYZ: : Rert', ': ', '456', ': tty: *']]'
                                 ^
SyntaxError: invalid syntax

How can I add a member list in this format !Join ['', ['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']] and it not having single or double quotes in the final YAML? 如何以这种格式添加成员列表!Join ['', ['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']]和最终的YAML中没有单引号或双引号吗?

You seem to be thinking that this: 您似乎在想:

!Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']]

is a plain scalar that gets loaded as a string in Python. 是一个普通标量,在Python中以字符串形式加载。 It is not. 它不是。 A plain scalar cannot start with an exclamation mark, as YAML uses that to introduces a tag , and what you load from there is quite a complicated object: 普通标量不能以感叹号开头,因为YAML会使用感叹号来引入标记 ,并且从那里加载的内容是一个非常复杂的对象:

import ruamel.yaml

yaml = ruamel.yaml.YAML()
data = yaml.load("!Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']]")
print(data)

prints: 印刷品:

['', ['abc:def:', <ruamel.yaml.comments.TaggedScalar object at 0x7f405265fa58>, ':', '123', ':tty:*']]

You can build such objects from scratch, but it is probably more easy, to load the list item you want to add, from its YAML string representation. 您可以从头开始构建此类对象,但从其YAML字符串表示形式加载要添加的列表项可能更容易。 If you want to engage in some DIY, you can construct it the entry from CommentedSeq and TaggedScalar instances as well: 如果你想从事一些DIY,你可以建,这从入门CommentedSeqTaggedScalar实例,以及:

import sys
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedSeq as cs
from ruamel.yaml.comments import TaggedScalar as ts
from ruamel.yaml.scalarstring import SingleQuotedScalarString as sq

yaml_doc = """\
Condition:
  Like:
    - !Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']]
"""
yaml = YAML()
yaml.preserve_quotes = True
yaml.width = 4096
data = yaml.load(yaml_doc)
ref = data['Condition']['Like']

new_member = yaml.load("!Join ['',['abc:def:', !Ref 'XYZ::Rert', ':', '456', ':tty:*']]")

ts1 = ts()
ts1.value = 'XYZ::Rert'
ts1.style = "'"
ts1.yaml_set_tag('!Ref')

new_member2 = cs([sq(''), [sq('abc:def:'), ts1 , sq(':'), sq('789'), sq(':tty:*')]])
new_member2.yaml_set_tag('!Join')
new_member2.fa.set_flow_style()

ref.append(new_member)
ref.append(new_member2)

yaml.dump(data, sys.stdout)

which gives: 这使:

Condition:
  Like:
  - !Join ['', ['abc:def:', !Ref 'XYZ::Rert', ':', '123', ':tty:*']]
  - !Join ['', ['abc:def:', !Ref 'XYZ::Rert', ':', '456', ':tty:*']]
  - !Join ['', ['abc:def:', !Ref 'XYZ::Rert', ':', '789', ':tty:*']]

(If you really want to figure out how to construct something like new_member2 yourself, it helps to load the output you want from YAML and print the individual items, their types and their attributes) (如果您真的想弄清楚如何自己构造类似new_member2 ,则有助于从YAML加载所需的输出并打印各个项目,其类型和属性)

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

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