ruamel.yaml

Preserve quotes and also add data with quotes in Ruamel

旧城冷巷雨未停 提交于 2020-12-29 12:08:25
问题 I am using Ruamel to preserve quote styles in human-edited YAML files. I have example input data as: --- a: '1' b: "2" c: 3 I read in data using: def read_file(f): with open(f, 'r') as _f: return ruamel.yaml.round_trip_load(_f.read(), preserve_quotes=True) I then edit that data: data = read_file('in.yaml') data['foo'] = 'bar' I write back to disk using: def write_file(f, data): with open(f, 'w') as _f: _f.write(ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper, width=1024)) write_file

Preserve quotes and also add data with quotes in Ruamel

℡╲_俬逩灬. 提交于 2020-12-29 12:07:21
问题 I am using Ruamel to preserve quote styles in human-edited YAML files. I have example input data as: --- a: '1' b: "2" c: 3 I read in data using: def read_file(f): with open(f, 'r') as _f: return ruamel.yaml.round_trip_load(_f.read(), preserve_quotes=True) I then edit that data: data = read_file('in.yaml') data['foo'] = 'bar' I write back to disk using: def write_file(f, data): with open(f, 'w') as _f: _f.write(ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper, width=1024)) write_file

Preserve quotes and also add data with quotes in Ruamel

不羁岁月 提交于 2020-12-29 12:05:04
问题 I am using Ruamel to preserve quote styles in human-edited YAML files. I have example input data as: --- a: '1' b: "2" c: 3 I read in data using: def read_file(f): with open(f, 'r') as _f: return ruamel.yaml.round_trip_load(_f.read(), preserve_quotes=True) I then edit that data: data = read_file('in.yaml') data['foo'] = 'bar' I write back to disk using: def write_file(f, data): with open(f, 'w') as _f: _f.write(ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper, width=1024)) write_file

get comment during iteration in ruamel.yaml

匆匆过客 提交于 2020-12-13 04:28:41
问题 How can I get the comments when I iterate through the YAML object yaml = YAML() with open(path, 'r') as f: yaml_data = yaml.load(f) for obj in yaml_data: # how to get the comments here? This is the source data (an ansible playbook) --- - name: gather all complex custom facts using the custom module hosts: switches gather_facts: False connection: local tasks: # There is a bug in ansible 2.4.1 which prevents it loading # playbook/group_vars - name: ensure we're running a known working version

get comment during iteration in ruamel.yaml

给你一囗甜甜゛ 提交于 2020-12-13 04:28:11
问题 How can I get the comments when I iterate through the YAML object yaml = YAML() with open(path, 'r') as f: yaml_data = yaml.load(f) for obj in yaml_data: # how to get the comments here? This is the source data (an ansible playbook) --- - name: gather all complex custom facts using the custom module hosts: switches gather_facts: False connection: local tasks: # There is a bug in ansible 2.4.1 which prevents it loading # playbook/group_vars - name: ensure we're running a known working version

How to recursively sort YAML using CommentedMap?

一曲冷凌霜 提交于 2020-08-10 04:22:58
问题 There's a good example of sorting at the top level, but how would you recurse the sort at all levels? This seems to work: def sort_commentedmap(self, od): res = ruamel.yaml.comments.CommentedMap() for k in sorted(od): res[k] = od[k] if not isinstance(od[k], ruamel.yaml.comments.CommentedSeq): continue for idx, i in enumerate(od[k]): if isinstance(i, str): res[k][int(idx)] = i else: res[k][int(idx)] = self.sort_commentedmap(i) return res Does this look correct? 回答1: In YAML you can have