问题是在Python中往返转储YAML时是否有保留顺序的方法?解决后,正确导入了数据结构,正确设置了样式属性,等等。我编写了一些代码来插入嵌套的CommentedMap()字典(sw-99-54321),其中包含一些字典和列表,例如:
dc01:
sw-06-50001: # existing entry
servers:
- {ip: 10.255.206.12, port: 50001, weight: 100}
- {ip: 10.255.206.13, port: 50001, weight: 90}
virtual: {ip: 192.168.1.4, port: 50001}
sw-99-54321: # the entry I am trying to add
servers:
- ip: 192.168.1.1
port: '65535'
weight: 100
- ip: 192.168.1.2
port: '65534'
weight: 90
virtual:
ip: 192.168.1.1
port: 65532
我想以与“ sw-06-50001”相同的方式来格式化“ sw-99-54321”。
我知道样式绑定到<var>.fa.__flow_style
属性,但是我不能直接更改它:
data['services']['dc01']['sw-99-54321']['servers'].fa.set_flow_style='True'
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'Format' object attribute 'set_flow_style' is read-only
是否可以设置它,或以其他方式为某些元素定义块样式?
由于您的示例具有流样式,因此您可以做两件事
1)明确设置流样式:
import sys
import ruamel.yaml
yaml_str = """\
dc01:
sw-06-50001: #existed entry
servers:
- {ip: 10.255.206.12, port: 50001, weight: 100}
- {ip: 10.255.206.13, port: 50001, weight: 90}
virtual: {ip: 192.168.1.4, port: 50001}
"""
yaml = ruamel.yaml.YAML()
# yaml.indent(mapping=4, sequence=4, offset=2)
# yaml.preserve_quotes = True
data = yaml.load(yaml_str)
data['dc01']['sw-99-54321'] = n = {}
n['servers'] = l = []
l.append(ruamel.yaml.comments.CommentedMap([('ip', '192.168.1.1'), ('port', '65535'), ('weight', 100)]))
l[-1].fa.set_flow_style()
l.append(ruamel.yaml.comments.CommentedMap([('ip', '192.168.1.2'), ('port', '65534'), ('weight', 90)]))
l[-1].fa.set_flow_style()
n['virtual'] = x = ruamel.yaml.comments.CommentedMap([('ip', '192.168.1.1'), ('port', '65532')])
x.fa.set_flow_style()
yaml.dump(data, sys.stdout)
这使:
dc01:
sw-06-50001: #existed entry
servers:
- {ip: 10.255.206.12, port: 50001, weight: 100}
- {ip: 10.255.206.13, port: 50001, weight: 90}
virtual: {ip: 192.168.1.4, port: 50001}
sw-99-54321:
servers:
- {ip: 192.168.1.1, port: '65535', weight: 100}
- {ip: 192.168.1.2, port: '65534', weight: 90}
virtual: {ip: 192.168.1.1, port: '65532'}
2)将所有叶节点设置为全局流样式:
import sys
import ruamel.yaml
yaml_str = """\
dc01:
sw-06-50001: #existed entry
servers:
- {ip: 10.255.206.12, port: 50001, weight: 100}
- {ip: 10.255.206.13, port: 50001, weight: 90}
virtual: {ip: 192.168.1.4, port: 50001}
"""
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = None
data = yaml.load(yaml_str)
data['dc01']['sw-99-54321'] = n = ruamel.yaml.comments.CommentedMap()
n['servers'] = l = []
l.append(ruamel.yaml.comments.CommentedMap([('ip', '192.168.1.1'), ('port', '65535'), ('weight', 100)]))
l.append(dict(ip='192.168.1.2', port='65534', weight=90))
n['virtual'] = ruamel.yaml.comments.CommentedMap([('ip', '192.168.1.1'), ('port', '65532')])
yaml.dump(data, sys.stdout)
这使:
dc01:
sw-06-50001: #existed entry
servers:
- {ip: 10.255.206.12, port: 50001, weight: 100}
- {ip: 10.255.206.13, port: 50001, weight: 90}
virtual: {ip: 192.168.1.4, port: 50001}
sw-99-54321:
servers:
- {ip: 192.168.1.1, port: '65535', weight: 100}
- {ip: 192.168.1.2, port: '65534', weight: 90}
virtual: {ip: 192.168.1.1, port: '65532'}
使用此方法,无需创建CommentedMap()
实例,可以使用常规命令
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句