简体   繁体   English

如何使用ruamel.yaml添加节点

[英]How to add a node with ruamel.yaml

I tried to add a new node following the example but: 我尝试在该示例之后添加一个新节点,但是:

myitems = ruamel.yaml.load(inp, ruamel.yaml.RoundTripLoader)
myitems['abc'].append('test')

gives me an error: 给我一个错误:

Traceback (most recent call last):
  File "item_updater.py", line 148, in <module>
    myitems['wohnung'].append('test')
AttributeError: 'CommentedMap' object has no attribute 'append'

I am using ruamel.yaml v0.13.7 我正在使用ruamel.yaml v0.13.7

What am I doing wrong? 我究竟做错了什么?

Your error doesn't come from the example you indicated, as in the inp of the example there is no wohnung that shows up in your error. 你的错误不是来自你指定的例子,如在inp的例子没有wohnung ,在你的错误出现。

You probably forgot a - somewhere: 您可能在某处忘记了-

wohnung:
   a: 1

instead of: 代替:

wohnung:
- a: 1

only on the latter you can append using myitems['wohnung'].append('test') . 仅在后者上,您可以使用myitems['wohnung'].append('test')

The example works, but without showing your real YAML input it is difficult to see what is the exact cause of your error. 该示例有效,但是没有显示您的真实YAML输入,很难看到导致错误的确切原因。

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

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