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

673 questions
389
votes
11 answers

How do I install the yaml package for Python?

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…
harperville
  • 5,730
  • 7
  • 23
  • 32
146
votes
2 answers

How can I write data in YAML format in a file?

I need to write the below data to yaml file using Python: {A:a, B:{C:c, D:d, E:e}} i.e., dictionary in a dictionary. How can I achieve this?
user1643521
  • 1,831
  • 3
  • 12
  • 6
137
votes
8 answers

In Python, how can you load YAML mappings as OrderedDicts?

I'd like to get PyYAML's loader to load mappings (and ordered mappings) into the Python 2.7+ OrderedDict type, instead of the vanilla dict and the list of pairs it currently uses. What's the best way to do that?
Eric Naeseth
  • 2,093
  • 2
  • 18
  • 18
80
votes
7 answers

How to upgrade disutils package PyYAML?

I was trying to install chatterbot which has a dependency on PyYAML=3.12. In my Ubuntu machine installed PyYAML version is 3.11. So I used the following command to upgrade PyYAML: sudo -H pip3 install --upgrade PyYAML But it gives the following…
sphoenix
  • 2,152
  • 2
  • 19
  • 30
71
votes
5 answers

pyyaml: dumping without tags

I have >>> import yaml >>> yaml.dump(u'abc') "!!python/unicode 'abc'\n" But I want >>> import yaml >>> yaml.dump(u'abc', magic='something') 'abc\n' What magic param forces no tagging?
Paul Tarjan
  • 44,540
  • 54
  • 163
  • 207
66
votes
2 answers

Building an array of dictionary items in YAML?

Basically trying to something in yaml that could be done using this json: { models: [ { model: "a" type: "x" #bunch of properties... }, { model: "b" type: "y" #bunch of properties... } ] } So far this is what I…
sadaf2605
  • 6,634
  • 8
  • 49
  • 87
65
votes
9 answers

Can PyYAML dump dict items in non-alphabetical order?

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…
mwcz
  • 7,885
  • 9
  • 38
  • 62
65
votes
1 answer

Reading YAML file with Python results in yaml.composer.ComposerError: expected a single document in the stream

I have a yaml file that looks like --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request: 341570 --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request: 341569 --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request:…
Sriharsha
  • 2,120
  • 1
  • 14
  • 19
57
votes
3 answers

Save/dump a YAML file with comments in PyYAML

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?
Harley Holcombe
  • 155,163
  • 15
  • 67
  • 62
47
votes
1 answer

I don't understand what a YAML tag is

I get it on some level, but I have yet to see an example that didn't bring up more questions than answers. http://rhnh.net/2011/01/31/yaml-tutorial # Set.new([1,2]).to_yaml --- !ruby/object:Set hash: 1: true 2: true I get that we're declaring…
Fred
  • 3,100
  • 6
  • 32
  • 49
42
votes
3 answers

PyYAML dump format

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…
nicosantangelo
  • 11,886
  • 3
  • 29
  • 44
41
votes
6 answers

ImportError: No module named 'yaml'

I have one script in which I am trying to execute python3 env/common_config/add_imagepullsecret.py But, I am getting the following error: [root@kevin]# python3 env/common_config/add_imagepullsecret.py Traceback (most recent call last): File…
Neeraj
  • 511
  • 1
  • 4
  • 3
34
votes
4 answers

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

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 =…
Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
32
votes
2 answers

How to force PyYAML to load strings as unicode objects?

The PyYAML package loads unmarked strings as either unicode or str objects, depending on their content. I would like to use unicode objects throughout my program (and, unfortunately, can't switch to Python 3 just yet). Is there an easy way to force…
Petr Viktorin
  • 58,535
  • 6
  • 72
  • 77
28
votes
2 answers

pyyaml is producing undesired !!python/unicode output

I am using pyyaml to dump an object to a file. There are several unicode strings in the object. I've done this before, but now it's producing output items like this: 'item': !!python/unicode "some string" Instead of the desired: 'item': 'some…
edA-qa mort-ora-y
  • 26,115
  • 34
  • 118
  • 238
1
2 3
44 45