新变量的字典是:
yaml_cluster = defaultdict(dict)我使用yaml库中的yaml_file在python中的一个变量中使用了这个safe_load:
domainInfo:
AdminUserName: '--FIX ME--'
AdminPassword: '--FIX ME--'
topology:
Name: 'wld-pil-10'
ConfigBackupEnabled: true
AdminServerName: 'wls-pil-10-sa-adm-n0'
DomainVersion: 12.2.1.4.0
ProductionModeEnabled: true
ArchiveConfigurationCount: 20
Cluster:
'test-bruno-jee-r01a-c01':
ClientCertProxyEnabled: true
WeblogicPluginEnabled: true
Server:
'test-bruno-jee-r01a-it-c01-m1-n1':
ListenPort: 10022
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
NMSocketCreateTimeoutInMillis: 30000
Machine: 'wlm-pil-10-n1'
'test-bruno-jee-r02a-it-c01-m1-n1':
ListenPort: 10025
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
NMSocketCreateTimeoutInMillis: 30000
Machine: 'wlm-pil-10-n2'
'wls-pil-10-sa-adm-n0':
ListenPort: 11030
WeblogicPluginEnabled: true
ClientCertProxyEnabled: true
Machine: 'wlm-pil-10-n0'如何选择其中的前两个?我试过,但没有成功:
servers = sorted(yaml_file["topology"]["Server"])[:-1]
for server in yaml_file["topology"]["Server"]:
if server in servers:
yaml_cluster["topology"]["Server"][server] = yaml_file["topology"]["Server"][server]错误:
致命: wls-pil-103-sa-adm-n0:=>!=>{“已更改”:真,"msg":“非零返回代码”,"rc":1,"stderr":“回溯(最近一次调用):\n File”/home/split_yaml.py,第40行,in \n
Yaml_cluster“拓扑”Server=yaml_file“拓扑”服务器#TypeError: string索引必须为整型\n nKeyError:‘Server’\n,"stderr_lines":[“回溯”(最近一次调用):",“,”文件“/home/split_yaml.py,第40行,在”,“
Yaml_cluster“拓扑”Server=yaml_file“#TypeError: string索引必须是整数”、"KeyError:'Server'"]、"stdout":"“、"stdout_lines":[]}
我认为这里有两个问题
yaml_cluster"topology""Server" =yaml_file“拓扑”服务器--“服务器”和yaml_cluster端的变量服务器。我认为字典不允许我分配那些值。
发布于 2021-01-21 16:23:35
问题是
yaml_cluster = defaultdict(dict)如果您执行yaml_cluster["topology"],并且该键不存在,那么defaultdict将使用给定的工厂来构造它。因此,它将返回一个空的dict。然后,对这个数据进行["Server"],而它并不存在。由于您现在有一个简单的dict,而不是defaultdict,这将导致一个KeyError。
改为这样做(taken from here):
nested_dict = lambda: defaultdict(nested_dict)
yaml_cluster = nested_dict()此外,您还可以将循环简化为
for server in servers:
yaml_cluster["topology"]["Server"][server] = yaml_file["topology"]["Server"][server]https://stackoverflow.com/questions/65829901
复制相似问题