ruamel.yaml

Transform attribute in yaml.dump is not working

对着背影说爱祢 提交于 2021-02-11 12:15:28
问题 If we want to alter the output of yaml.dump we can use tranform keyword argument. Documentation: https://yaml.readthedocs.io/en/latest/example.html Here is the yaml data: metadata: name: name alias: alias it is stored in variable x. x = 'metadata:\n name: name\n alias: alias\n' def tr(s): return s.replace('\n', '\n ') # Want 4 space at each new line from ruamel.yaml import YAML from ruamel.yaml.compat import StringIO yaml = YAML(typ="safe") yaml.default_flow_style = False stream = StringIO()

Preserving following comments when removing last dict key with ruamel.yaml

℡╲_俬逩灬. 提交于 2021-02-05 11:52:53
问题 I'm trying to use the ruamel.yaml Python library to remove some keys/value pairs from nested dictionaries within a large YAML file, while retaining surrounding comments. Here's a simplified version of the code I'm using: import sys import ruamel.yaml with open(sys.argv[1], 'r') as doc: parsed = ruamel.yaml.round_trip_load(doc, preserve_quotes=True) for item in parsed['items']: if item['color'] == 'blue': del item['color'] yaml = ruamel.yaml.YAML(typ='rt') yaml.indent(sequence=4, offset=2)

Preserving following comments when removing last dict key with ruamel.yaml

僤鯓⒐⒋嵵緔 提交于 2021-02-05 11:52:01
问题 I'm trying to use the ruamel.yaml Python library to remove some keys/value pairs from nested dictionaries within a large YAML file, while retaining surrounding comments. Here's a simplified version of the code I'm using: import sys import ruamel.yaml with open(sys.argv[1], 'r') as doc: parsed = ruamel.yaml.round_trip_load(doc, preserve_quotes=True) for item in parsed['items']: if item['color'] == 'blue': del item['color'] yaml = ruamel.yaml.YAML(typ='rt') yaml.indent(sequence=4, offset=2)

Is there a way to preserve order while round-trip dumping YAML in Python?

人盡茶涼 提交于 2021-01-28 07:54:55
问题 I am trying to load some data from a YAML-file and put it back: services: dc01: sw-06-50001: 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-09-50002: servers: - {ip: 10.255.206.18, port: 50002, weight: 100} - {ip: 10.255.206.19, port: 50002, weight: 90} virtual: {ip: 192.168.1.4, port: 50002} sw-06-50003: servers: - {ip: 10.255.206.12, port: 50003, weight: 100} - {ip: 10.255.206.13, port: 50003

How can I add a comment with ruamel.yaml

Deadly 提交于 2021-01-27 20:01:56
问题 I'm trying to create a data structure with ruamel.yaml and want to add comments before dumping and/or loading it again. Unfortunately all examples load some string with the round trip dumper first or use no longer existing APIs. This is what I am trying to dump: test: asdf # Test Comment! I tried the following: insert = ruamel.yaml.comments.CommentedMap() start_mark = ruamel.yaml.error.CommentMark(0) insert['test'] = 'asdf' insert.ca.items['test'] = [ None, [ruamel.yaml.CommentToken(value='#

Disabling alias for yaml file in python

穿精又带淫゛_ 提交于 2021-01-27 18:25:03
问题 I have a problem that I wish to prevent aliases from occurring in my YAML file. Is there anyway I can disable the aliases in the YAML file generated, to achieve the intended output? The current YAML file I have is this below: agents: - start: [0, 0] goal: [2, 0] name: agent0 - start: [2, 0] goal: [0, 0] name: agent1 map: dimensions: [3, 3] obstacles: - !!python/tuple [0, 1] - !!python/tuple [2, 1] As I updating the YAML file for each of the agents whenever they reached their goal point with

Ruamel.yaml: How to access merge keys and comments in loaded OrderedDict

时光怂恿深爱的人放手 提交于 2021-01-05 12:43:10
问题 I have a Python program that is parsing a number of YAML files, some containing comments, anchors, references, and merge keys that I'd like preserved when I load the YAML file into my parser. ruamel.yaml seems to have round-trip preservation of these when I run the following: with open(yaml_file, "r") as f: yaml = f.read() parsed_yaml = ruamel.yaml.load(yaml, ruamel.yaml.RoundTripLoader) print ruamel.yaml.dump(parsed_yaml,Dumper=ruamel.yaml.RoundTripDumper) Which prints out the original file

Using ruamel.yaml, how can I make vars with NEWLINEs be multiline without quotes

萝らか妹 提交于 2021-01-03 06:44:31
问题 I am generating YAML which serves as a protocol, and it contains some generated JSON inside of it. import json from ruamel import yaml jsonsample = { "id": "123", "type": "customer-account", "other": "..." } myyamel = {} myyamel['sample'] = {} myyamel['sample']['description'] = "This example shows the structure of the message" myyamel['sample']['content'] = json.dumps( jsonsample, indent=4, separators=(',', ': ')) print yaml.round_trip_dump(myyamel, default_style = None, default_flow_style

Using ruamel.yaml, how can I make vars with NEWLINEs be multiline without quotes

一个人想着一个人 提交于 2021-01-03 06:44:13
问题 I am generating YAML which serves as a protocol, and it contains some generated JSON inside of it. import json from ruamel import yaml jsonsample = { "id": "123", "type": "customer-account", "other": "..." } myyamel = {} myyamel['sample'] = {} myyamel['sample']['description'] = "This example shows the structure of the message" myyamel['sample']['content'] = json.dumps( jsonsample, indent=4, separators=(',', ': ')) print yaml.round_trip_dump(myyamel, default_style = None, default_flow_style

Preserve quotes and also add data with quotes in Ruamel

て烟熏妆下的殇ゞ 提交于 2020-12-29 12:09:57
问题 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