python写入yanl文件之前添加注释
注释的方法
yaml_set_comment_before_after_key(key,before='123',after='123',after_indent=2) 某个key上面和下面(注意缩进)添加注释
yaml_set_start_comment('123') 在某key的开始添加注释
yaml_add_eol_comment('123',column=12) 在某个key后面添加注释
yaml格式示例
groups:
- name: alertgroupname
rules:
- alert: alertname
expr: expression
for: 1s
labels:
isMerge: 'true'
annotations:
summary: "summary"
value: '{{$value}}'
#尝试给yaml添加注释 yml_data = yaml.round_trip_load(yaml.round_trip_dump(tempDic)) for data in yml_data: #在标题后添加注释 #yml_data[data].yaml_add_eol_comment('外部接入告警', column=12) #在标题下面加入 #yml_data[data][0]['rules'].yaml_set_start_comment('外部接入告警', indent=2) rule = yml_data[data][0]['rules'] length = len(rule) start_num = new_or_old_flag + length for i in range(start_num): if desc[i] != '': rule[i].yaml_set_start_comment(desc[i], indent=2)
groups:
- name: alertgroupname
rules:
# alert comment
- alert: alertname
expr: expression
for: 1s
labels:
isMerge: 'true'
annotations:
summary: "summary"
value: '{{$value}}'