【发布时间】:2019-05-05 18:00:45
【问题描述】:
我正在使用 ruamel.yaml 插入一些值,但它将值附加在空格之后而不是空格之前。当前代码将值附加在行空间之后,如下面的 YAML 文件所示。标记为 ** NEW VALUE INSERTED HERE**
的 YAML 输出prefix_state:
v4:
8.8.8.8/32:
description: GOOGLE_DNS
enabled: true
tags:
- dns
community:
lb:
- SELF_NO_EXPORT
- TELCOM_NO_EXPORT
- BUSINESS_NO_EXPORT
**- <NEW VALUE INSERTED HERE>**
10.10.1.0/24:
description: SELF_LOCAL
enabled: True
tags:
- local
community:
lb:
- <NEW VALUE INSERTED HERE>
用于在 yaml 文件中附加值的代码如下:
yamldata=yaml.load(prefix_state_data,Loader=yaml.RoundTripLoader)
for arg in argv:
if arg is None:
pass
else:
for i in yamldata['prefix_state']['v4']:
if yamldata['prefix_state']['v4'][i]['community']['lb'] is not None:
yamldata['prefix_state']['v4'][i]['community']['lb'].append(arg+'_NO_EXPORT')
else:
yamldata['prefix_state']['v4'][i]['community']['lb']=[arg+'_NO_EXPORT']```
Expected end result is as below:
v4:
8.8.8.8/32:
description: GOOGLE_DNS
enabled: true
tags:
- dns
community:
lb:
- SELF_NO_EXPORT
- TELECOM_NO_EXPORT
- BUSINESS_NO_EXPORT
**- <NEW VALUE INSERTED HERE>**
10.10.1.0/24:
description: SELF_LOCAL
enabled: True
tags:
- local
community:
lb:
- <NEW VALUE INSERTED HERE>
【问题讨论】:
-
很难看出区别。
-
如果您查看第一个 yml 文件。它在 BUSINESS_NO_EXPORT 之后显示一行,然后是新社区但我期望的是空白行之前的新值。
标签: python ruamel.yaml