将重复的键值对附加到 YAML 中的嵌套字典
Append duplicate key value pair to nested dictionary in YAML
我正在尝试通过 python 脚本将重复键:值对附加到 YAML 文件中的嵌套字典。以下是我为实现此目的而编写的代码片段:
import click
import ruamel.yaml
def organization():
org_num = int(input("Please enter the number of organizations to be created: "))
org_val = 0
while org_val!= org_num:
print ("")
print("Please enter values to create Organizations")
print ("")
for org in range(org_num):
organization.org_name = str(raw_input("Enter the Organization Name: "))
organization.org_description = str(raw_input("Enter the Description of Organization: "))
print ("")
if click.confirm("Organization Name: "+ organization.org_name + "\nDescription: "+ organization.org_description + "\nIs this Correct?", default=True):
if org_val == 0:
org_val = org_val + 1
yaml = ruamel.yaml.YAML()
org_data = dict(
organizations=dict(
name=organization.org_name,
description=organization.org_description,
)
)
with open('input.yml', 'a') as outfile:
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.dump(org_data, outfile)
else:
org_val = org_val + 1
yaml = ruamel.yaml.YAML()
org_data = dict(
name=organization.org_name,
description=organization.org_description,
)
with open('input.yml', 'r') as yamlfile:
cur_yaml = yaml.load(yamlfile)
cur_yaml['organizations'].update(org_data)
if cur_yaml:
with open('input.yml','w') as yamlfile:
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.dump(cur_yaml, yamlfile)
return organization.org_name, organization.org_description
organization()
在 python 脚本的末尾,我的 input.yml
文件应如下所示:
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations:
- description: xyz
name: abc
- description: pqr
name: def
然而,每次脚本 运行 时,它不会将值附加到组织,而是覆盖它。
我也尝试过使用追加而不是更新,但出现以下错误:
AttributeError: 'CommentedMap' object has no attribute 'append'
我该怎么做才能解决这个问题?
此外,由于我是开发新手,任何关于改进此代码的建议都将非常有帮助。
如果我没理解错的话,你要的是cur_yaml['organizations'] += [org_data]
.
请注意,如果您多次 运行 脚本,您将多次获得相同的条目。
使用 update
是行不通的,因为键的值
organizations
是一个序列并加载为类似 list
的类型
CommentedSeq
。所以 append
-ing 是正确的做法。
这行不通有点不清楚,因为您没有提供该输入
您开始的代码,也不是执行 append
ing 时使用的代码
CommentedMap
.
上的 AttributeError
如果您有一个组织并添加另一个组织,则以下方法有效:
import sys
import ruamel.yaml
yaml_str = """\
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations:
- description: xyz
name: abc
"""
org_data = dict(
description='pqr',
name='def',
)
yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4, sequence=4, offset=2)
cur_yaml = yaml.load(yaml_str)
cur_yaml['organizations'].append(org_data)
yaml.dump(cur_yaml, sys.stdout)
这给出:
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations:
- description: xyz
name: abc
- description: pqr
name: def
如果您还没有组织,请确保您输入的 YAML 如下所示:
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations: []
在旧版本的 Python 中,您添加的数据中键的顺序无法保证。到
在旧版本上也执行该命令:
org_data = ruamel.yaml.comments.CommentedMap((('description', 'pqr'), ('name', 'def')))
或
org_data = ruamel.yaml.comments.CommentedMap()
org_data['description'] = 'pqr'
org_data['name'] = 'def'
找到了问题并且现在工作正常。由于 name 和 description 是组织的列表对象,我在下面的代码中添加了 [] 并且它开始工作了。
org_data = dict(
organizations=[dict(
name=tower_organization.org_name,
description=tower_organization.org_description,
)
]
)
除上述之外,我猜想 append 不起作用是因为第一个对象中缺少连字符“-”作为标识的一部分。修复以上代码后,append 也可以正常工作了。
谢谢大家的回答。
我正在尝试通过 python 脚本将重复键:值对附加到 YAML 文件中的嵌套字典。以下是我为实现此目的而编写的代码片段:
import click
import ruamel.yaml
def organization():
org_num = int(input("Please enter the number of organizations to be created: "))
org_val = 0
while org_val!= org_num:
print ("")
print("Please enter values to create Organizations")
print ("")
for org in range(org_num):
organization.org_name = str(raw_input("Enter the Organization Name: "))
organization.org_description = str(raw_input("Enter the Description of Organization: "))
print ("")
if click.confirm("Organization Name: "+ organization.org_name + "\nDescription: "+ organization.org_description + "\nIs this Correct?", default=True):
if org_val == 0:
org_val = org_val + 1
yaml = ruamel.yaml.YAML()
org_data = dict(
organizations=dict(
name=organization.org_name,
description=organization.org_description,
)
)
with open('input.yml', 'a') as outfile:
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.dump(org_data, outfile)
else:
org_val = org_val + 1
yaml = ruamel.yaml.YAML()
org_data = dict(
name=organization.org_name,
description=organization.org_description,
)
with open('input.yml', 'r') as yamlfile:
cur_yaml = yaml.load(yamlfile)
cur_yaml['organizations'].update(org_data)
if cur_yaml:
with open('input.yml','w') as yamlfile:
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.dump(cur_yaml, yamlfile)
return organization.org_name, organization.org_description
organization()
在 python 脚本的末尾,我的 input.yml
文件应如下所示:
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations:
- description: xyz
name: abc
- description: pqr
name: def
然而,每次脚本 运行 时,它不会将值附加到组织,而是覆盖它。
我也尝试过使用追加而不是更新,但出现以下错误:
AttributeError: 'CommentedMap' object has no attribute 'append'
我该怎么做才能解决这个问题?
此外,由于我是开发新手,任何关于改进此代码的建议都将非常有帮助。
如果我没理解错的话,你要的是cur_yaml['organizations'] += [org_data]
.
请注意,如果您多次 运行 脚本,您将多次获得相同的条目。
使用 update
是行不通的,因为键的值
organizations
是一个序列并加载为类似 list
的类型
CommentedSeq
。所以 append
-ing 是正确的做法。
这行不通有点不清楚,因为您没有提供该输入
您开始的代码,也不是执行 append
ing 时使用的代码
CommentedMap
.
AttributeError
如果您有一个组织并添加另一个组织,则以下方法有效:
import sys
import ruamel.yaml
yaml_str = """\
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations:
- description: xyz
name: abc
"""
org_data = dict(
description='pqr',
name='def',
)
yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4, sequence=4, offset=2)
cur_yaml = yaml.load(yaml_str)
cur_yaml['organizations'].append(org_data)
yaml.dump(cur_yaml, sys.stdout)
这给出:
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations:
- description: xyz
name: abc
- description: pqr
name: def
如果您还没有组织,请确保您输入的 YAML 如下所示:
version: x.x.x
is_enterprise: 'true'
license: secrets/license.txt
organizations: []
在旧版本的 Python 中,您添加的数据中键的顺序无法保证。到 在旧版本上也执行该命令:
org_data = ruamel.yaml.comments.CommentedMap((('description', 'pqr'), ('name', 'def')))
或
org_data = ruamel.yaml.comments.CommentedMap()
org_data['description'] = 'pqr'
org_data['name'] = 'def'
找到了问题并且现在工作正常。由于 name 和 description 是组织的列表对象,我在下面的代码中添加了 [] 并且它开始工作了。
org_data = dict(
organizations=[dict(
name=tower_organization.org_name,
description=tower_organization.org_description,
)
]
)
除上述之外,我猜想 append 不起作用是因为第一个对象中缺少连字符“-”作为标识的一部分。修复以上代码后,append 也可以正常工作了。
谢谢大家的回答。