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
23
votes
6 answers

Python and PYAML - yaml.scanner.ScannerError: mapping values are not allowed here

I am on ubunty 64 with python 2.7 and using PyYAML-3.10 Below is my yaml file: host:localhost username:root password:test database:test operations_database:operations treeroot: branch1: name: Node 1 branch1-1: name:…
user959129
22
votes
2 answers

Preserve new lines in YAML

How do I format a YAML document like this so that PyYAML can parse it properly? Data: Some data, here and a special character like ':' Another line of data on a separate line I know that the ':' character is special so I have to surround the…
Coding District
  • 11,901
  • 4
  • 26
  • 30
22
votes
2 answers

Adding comments to YAML produced with PyYaml

I'm creating Yaml documents from my own python objects using PyYaml. for example my object: class MyObj(object): name = "boby" age = 34 becomes: boby: age: 34 So far so good. But I have not found a way to programmatically add…
Periodic Maintenance
  • 1,698
  • 4
  • 20
  • 32
21
votes
2 answers

How to parse a YAML file with multiple documents?

Here is my parsing code: import yaml def yaml_as_python(val): """Convert YAML to dict""" try: return yaml.load_all(val) except yaml.YAMLError as exc: return exc with open('circuits-small.yaml','r') as input_file: …
BigBoy1337
  • 4,735
  • 16
  • 70
  • 138
20
votes
3 answers

Parse an AWS CloudFormation template with the PyYAML library

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…
nixmind
  • 2,060
  • 6
  • 32
  • 54
20
votes
2 answers

pretty output with pyyaml

I have a python project where I'd like to use YAML (pyYaml 3.11), particularly because it is "pretty" and easy for users to edit in a text editor if and when necessary. My problem, though, is if I bring the YAML into a python application (as I will…
Mayur Patel
  • 945
  • 1
  • 7
  • 15
19
votes
4 answers

yaml.dump adding unwanted newlines in multiline strings

I have a multiline string: >>> import credstash >>> d = credstash.getSecret('alex_test_key', region='ap-southeast-2') To see the raw data (first 162 characters): >>> credstash.getSecret('alex_test_key', region='ap-southeast-2')[0:162] u'-----BEGIN…
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
19
votes
3 answers

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

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…
scicalculator
  • 1,498
  • 3
  • 16
  • 33
18
votes
1 answer

Python difference between yaml.load and yaml.safe_load

I am seeing that PyYaml, truncates zero's while loading from yaml file, if one uses: yaml.safe_load(stream). It can be fixed, if one uses yaml.load(stream, Loader=yaml.BaseLoader), but is that advisable? It works with yaml.load and zeros are not…
iDev
  • 2,163
  • 10
  • 39
  • 64
18
votes
5 answers

PyYAML : Control ordering of items called by yaml.load()

I have a yaml setting file which creates some records in db: setting1: name: [item,item] name1: text anothersetting2: name: [item,item] sub_setting: name :[item,item] when i update this file with setting3 and regenerate records in db…
zzart
  • 11,207
  • 5
  • 52
  • 47
17
votes
2 answers

Parse yaml into a list in python

I am required to use YAML for a project. I have a YAML file which I am using to populate a list in a Python program which interacts with the data. My data looks like this: Employees: custid: 200 user: Ash - Smith - Cox I…
johnfk3
  • 469
  • 2
  • 5
  • 15
16
votes
4 answers

Getting duplicate keys in YAML using Python

We are in need of parsing YAML files which contain duplicate keys and all of these need to be parsed. It is not enough to skip duplicates. I know this is against the YAML spec and I would like to not have to do it, but a third-party tool used by us…
jakubka
  • 706
  • 1
  • 9
  • 23
16
votes
5 answers

PyYAML automatically converting certain keys to boolean values

I've been working with a the PyYAML parser for a few months now to convert file types as part of a data pipeline. I've found the parser to be quite idiosyncratic at times and it seems that today I've stumbled on another strange behavior. The file…
sulimmesh
  • 693
  • 1
  • 6
  • 23
15
votes
3 answers

How can I add a python tuple to a YAML file using pyYAML?

The title is fairly self-explanatory. When I save a tuple to a YAML file, I get something that looks like this: ambient: !!python/tuple [0.3, 0.3 ,0.3] When I try to load it with yaml.safe_load(file_object), I keep getting an error that…
Louis Thibault
  • 20,240
  • 25
  • 83
  • 152
15
votes
3 answers

Parsing Yaml in Python: Detect duplicated keys

The yaml library in python is not able to detect duplicated keys. This is a bug that has been reported years ago and there is not a fix yet. I would like to find a decent workaround to this problem. How plausible could be to create a regex that…
Tk421
  • 6,196
  • 6
  • 38
  • 47
1 2
3
55 56