Questions tagged [pyyaml]

PyYAML is a YAML 1.1 parser and emitter for Python. Use this tag for PyYAML specific python questions.

PyYAML is a parser/dumper for YAML 1.1 for the Python programming language.

PyYAML does not yet implemented the feature changes from the YAML 1.2 specification released in 2009.

See http://pyyaml.org/wiki/PyYAML for background and documentation.

Source code: https://github.com/yaml/pyyaml

828 questions
9
votes
2 answers

How to load a pyYAML file and access it using attributes instead of using the dictionary notation?

I have an YAML config that looks like: config: - id: foo - name: bar content: - run: xxx - remove: yyy I am using Python YAML module to load it but I want to access it in better ways like: stream = open(filename) config = load(stream,…
sorin
  • 161,544
  • 178
  • 535
  • 806
8
votes
1 answer

Is it possible to preserve YAML block structure when dumping a parsed document?

We use PyYAML to prep config files for different environments. But our YAML blocks lose integrity. Give input.yml ... pubkey: | -----BEGIN PUBLIC KEY----- MIGfMA0GCSq7OPxRrQEBAQUAA4GNADCBiQKBgQCvRVUKp6pr4qBEnE9lviuyfiNq …
Chris Betti
  • 2,721
  • 2
  • 27
  • 36
8
votes
2 answers

How to prevent re-definition of keys in YAML?

Is there any way to cause yaml.load to raise an exception whenever a given key appears more than once in the same dictionary? For example, parsing the following YAML would raise an exception, because some_key appears twice: { some_key: 0, …
kjo
  • 33,683
  • 52
  • 148
  • 265
8
votes
5 answers

How to install pyYAML on windows 10

Im trying to install pyYAML from source on windows 10. I downloaded PyYAML 3.11 from https://pypi.python.org/pypi/PyYAML. When I run setup.py I get error: [WinError 2] The system cannot find the file specified. How to fix it? PyYAML-3.11>python…
Kamrul Khan
  • 3,260
  • 4
  • 32
  • 59
8
votes
4 answers

Load YAML nested with Jinja2 in Python

I have a YAML file (all.yaml) that looks like: ... var1: val1 var2: val2 var3: {{var1}}-{{var2}}.txt ... If I load it in Python like this: import yaml f = open('all.yaml') dataMap = yaml.safe_load(f) f.close() print(dataMap["var3"]) the output…
Sandro Koch
  • 303
  • 1
  • 4
  • 11
8
votes
2 answers

Different YAML array representations

I'm writing a file type converter using Python and PyYAML for a project where I am translating to and from YAML files multiple times. These file are then used by a separate service that I have no control over, so I need to translate back the YAML…
sulimmesh
  • 693
  • 1
  • 6
  • 23
8
votes
1 answer

Is it possible to anchor a Literal Block in YAML?

What I'm doing is creating several Literal Blocks like the one below, and putting them on a list. literal1: | line of text and stuff literal2: | ... and now the part which I can't figure out is to put them on a list. I've figured…
user735977
8
votes
2 answers

When to use YAML instead of JSON

YAML appears to be more readable than a JSON formatted object, apart from readability what advantages or disadvantages does PyAML have instead of JSON? How should we make a decision between the two, note that I am not asking the difference between…
stackit
  • 3,036
  • 9
  • 34
  • 62
8
votes
2 answers

PyYAML, how to align map entries?

I use PyYAML to output a python dictionary to YAML format: import yaml d = { 'bar': { 'foo': 'hello', 'supercalifragilisticexpialidocious': 'world' } } print yaml.dump(d, default_flow_style=False) The output is: bar: foo: hello …
mouviciel
  • 66,855
  • 13
  • 106
  • 140
7
votes
1 answer

PyYAML error: Could not determine a constructor for the tag '!vault'

I am trying to read a YAML file that has the tag !vault in it. I get the error: could not determine a constructor for the tag '!vault' Upon reading a couple of blogs, I understood that I need to specify some constructors to resolve this issue, but…
Eva
  • 515
  • 4
  • 28
7
votes
3 answers

Is 'yes' really an alias for 'true' according to the YAML 1.1 spec? The 1.2 spec?

I'm trying to debug an issue, and it boils down to... >>> import yaml as pyyaml >>> import ruamel.yaml as ruamel >>> ruamel.load("x: yes") == ruamel.load("x: true") False >>> pyyaml.safe_load("x: yes") == pyyaml.safe_load("x: true") True There are…
jberryman
  • 16,334
  • 5
  • 42
  • 83
7
votes
2 answers

Add multiple docs in yaml file | PyYAML

I am working on an object where first python reads YAML, does some changes and then writes them back to file. Loading and updating values part is working fine but when I go to write the file it makes lists rather separate…
Ahsan Naseem
  • 1,046
  • 1
  • 19
  • 38
7
votes
1 answer

PyYAML dump Python object without tags

How can I dump Python objects without tags using PyYAML? I have such class: class Monster(yaml.YAMLObject): yaml_tag = u'!Monster' def __init__(self, name, hp, ac, attacks): self.name = name self.hp = hp self.ac = ac …
Oleksandr Yarushevskyi
  • 2,789
  • 2
  • 17
  • 24
7
votes
2 answers

PyYAML: load and dump yaml file and preserve tags ( !CustomTag )

I want to create a YAML filter that reads a YAML file, processes it and dumps it afterwards. It must resolve any aliases (that works already nicely out of the box): >>> yaml.dump(yaml.load(""" Foo: &bar name: bar Foo2: <<: *bar """)) 'Foo:…
Jan
  • 2,803
  • 6
  • 36
  • 57
7
votes
1 answer

PyYAML dumping boolean

I am loading a yaml True/False item(example below) from a YAML file. gzip: False This is correctly interpreted in a Jinja2 template as boolean True. The same YAML file is being read by another script, passed over as a python CGI form data and…
Anoop P Alias
  • 373
  • 1
  • 6
  • 15