我尝试在 example 之后添加一个新节点但是:
myitems = ruamel.yaml.load(inp, ruamel.yaml.RoundTripLoader)
myitems['abc'].append('test')
给我一个错误:
Traceback (most recent call last):
File "item_updater.py", line 148, in <module>
myitems['wohnung'].append('test')
AttributeError: 'CommentedMap' object has no attribute 'append'
我正在使用 ruamel.yaml v0.13.7
我做错了什么?
请您参考如下方法:
您的错误并非来自您指出的示例,因为在示例的 inp 中没有出现在您的错误中的 wohnung。
您可能在某处忘记了 -:
wohnung:
a: 1
代替:
wohnung:
- a: 1
只有在后者上你可以使用myitems['wohnung'].append('test')追加。
该示例有效,但如果不显示您的真实 YAML 输入,则很难看出错误的确切原因。
