我有一个文件 *.yaml内容如下:
bugs_tree:
bug_1:
html_arch: filepath
moved_by: user1
moved_date: '2018-01-30'
sfx_id: '1'
Run Code Online (Sandbox Code Playgroud)
我想在节点下的这个文件中添加一个新的子元素 [bugs_tree]
我尝试执行此操作如下所示:
if __name__ == "__main__":
new_yaml_data_dict = {
'bug_2': {
'sfx_id': '2',
'moved_by': 'user2',
'moved_date': '2018-01-30',
'html_arch': 'filepath'
}
}
with open('bugs.yaml','r') as yamlfile:
cur_yaml = yaml.load(yamlfile)
cur_yaml.extend(new_yaml_data_dict)
print(cur_yaml)
Run Code Online (Sandbox Code Playgroud)
然后文件应该看起来:
bugs_tree:
bug_1:
html_arch: filepath
moved_by: username
moved_date: '2018-01-30'
sfx_id: '1234'
bug_2:
html_arch: filepath
moved_by: user2
moved_date: '2018-01-30'
sfx_id: '2'
Run Code Online (Sandbox Code Playgroud)
当我尝试执行.append()OR .extend()OR 时.insert()出现错误
cur_yaml.extend(new_yaml_data_dict)
AttributeError: 'dict' object has no attribute 'extend'
Run Code Online (Sandbox Code Playgroud)