pyyaml

pyYAML Errors on “!” in a string

≡放荡痞女 提交于 2019-11-28 10:59:16
First, a disclaimer: I'm not too familiar with YAML. I'm trying to parse a YAML doc into Key Value Pairs (don't worry about how I'm doing it. I've got that bit handled) My file used to look something like: world: people: name:Suzy address:chez-bob Then, someone went and changed it. world: people: name:!$uzy address:chez-bob And I get this parse error: yaml.constructor.ConstructorError: could not determine a constructor for the tag '!$uzy' What does this even mean? How would I go about getting it to just interpret !$ as just two characters? I just want a dictionary of string keys and values!

Save/dump a YAML file with comments in PyYAML

旧街凉风 提交于 2019-11-28 06:43:24
I have a yaml file that looks like this: # The following key opens a door key: value Is there a way I can load and dump this data while maintaining the comment? phihag PyYAML throws away comments at a very low level (in Scanner.scan_to_next_token ). While you could adapt or extend it to handle comments in its whole stack, this would be a major modification. Dump ing (=emitting) comments seems to be easier and was discussed in ticket 114 on the old PyYAML bug tracker. If you are using block structured YAML, you can use the python package¹ ruamel.yaml which is a derivative of PyYAML and supports

Is there a way to construct an object using PyYAML construct_mapping after all nodes complete loading?

对着背影说爱祢 提交于 2019-11-28 02:40:38
问题 I am trying to make a yaml sequence in python that creates a custom python object. The object needs to be constructed with dicts and lists that are deconstructed after __init__ . However, it seems that the construct_mapping function does not construct the entire tree of embedded sequences (lists) and dicts. Consider the following: import yaml class Foo(object): def __init__(self, s, l=None, d=None): self.s = s self.l = l self.d = d def foo_constructor(loader, node): values = loader.construct

Parse an AWS CloudFormation template with the PyYAML library

删除回忆录丶 提交于 2019-11-27 19:21:03
问题 I am writing a custom Python application using the PyYAML library that needs to read in AWS CloudFormation YAML templates. I know the templates are valid CloudFormation templates, because I tested them using validate-template: ▶ aws cloudformation validate-template --template-body file://cloudformation.yml When I try to read them using the PyYAML library, however, I get errors like: yaml.scanner.ScannerError: mapping values are not allowed here and could not determine a constructor for the

Can PyYAML dump dict items in non-alphabetical order?

孤人 提交于 2019-11-27 19:19:01
I'm using yaml.dump to output a dict. It prints out each item in alphabetical order based on the key. >>> d = {"z":0,"y":0,"x":0} >>> yaml.dump( d, default_flow_style=False ) 'x: 0\ny: 0\nz: 0\n' Is there a way to control the order of the key/value pairs? In my particular use case, printing in reverse would (coincidentally) be good enough. For completeness though, I'm looking for an answer that shows how to control the order more precisely. I've looked at using collections.OrderedDict but PyYAML doesn't (seem to) support it. I've also looked at subclassing yaml.Dumper , but I haven't been able

Can PyYAML dump dict items in non-alphabetical order?

梦想的初衷 提交于 2019-11-27 04:04:15
问题 I'm using yaml.dump to output a dict. It prints out each item in alphabetical order based on the key. >>> d = {"z":0,"y":0,"x":0} >>> yaml.dump( d, default_flow_style=False ) 'x: 0\ny: 0\nz: 0\n' Is there a way to control the order of the key/value pairs? In my particular use case, printing in reverse would (coincidentally) be good enough. For completeness though, I'm looking for an answer that shows how to control the order more precisely. I've looked at using collections.OrderedDict but

How can I control what scalar form PyYAML uses for my data?

流过昼夜 提交于 2019-11-27 01:27:41
I've got an object with a short string attribute, and a long multi-line string attribute. I want to write the short string as a YAML quoted scalar, and the multi-line string as a literal scalar: my_obj.short = "Hello" my_obj.long = "Line1\nLine2\nLine3" I'd like the YAML to look like this: short: "Hello" long: | Line1 Line2 Line3 How can I instruct PyYAML to do this? If I call yaml.dump(my_obj) , it produces a dict-like output: {long: 'line1 line2 line3 ', short: Hello} (Not sure why long is double-spaced like that...) Can I dictate to PyYAML how to treat my attributes? I'd like to affect both

PyYAML dump format

只愿长相守 提交于 2019-11-26 22:47:26
问题 I know there are a few questions about this on SO, but I couldn't find what I was looking for. I'm using pyyaml to read ( .load() ) a .yml file, modify or add a key, and then write it ( .dump() ) again. The problem is that I want to keep the file format post-dump, but it changes. For example, I edit the key en.test.index.few to say "Bye" instead of "Hello" Python: with open(path, 'r', encoding = "utf-8") as yaml_file: self.dict = pyyaml.load(yaml_file) Then, afther changing the key: with open

How do I install the yaml package for Python?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 21:22:58
I have a Python program that uses YAML. I attempted to install it on a new server using pip install yaml and it returns the following: $ sudo pip install yaml Downloading/unpacking yaml Could not find any downloads that satisfy the requirement yaml No distributions at all found for yaml Storing complete log in /home/pa/.pip/pip.log How do I install the yaml package for Python? I'm running Python 2.7. (OS: Debian Wheezy) Bonlenfum You could try the search feature in pip, $ pip search yaml which looks for packages in PyPI with yaml in the short description. That reveals various packages,

Any yaml libraries in Python that support dumping of long strings as block literals or folded blocks?

自古美人都是妖i 提交于 2019-11-26 15:55:32
问题 I'd like to be able to dump a dictionary containing long strings that I'd like to have in the block style for readability. For example: foo: | this is a block literal bar: > this is a folded block PyYAML supports the loading of documents with this style but I can't seem to find a way to dump documents this way. Am I missing something? 回答1: import yaml class folded_unicode(unicode): pass class literal_unicode(unicode): pass def folded_unicode_representer(dumper, data): return dumper.represent