Danylo Korostil
Danylo Korostil

Reputation: 1484

Yaml python parcing for multi values

Given test.yml:

Step1:
    input: dd if=/dev/urandom of=/dev/null count=8192 bs=8192
    output: 8192+0 records in
    output: 8192+0 records out

Plus python script to read it:

import yaml

stream = open("test.yml", 'r')
Steps = yaml.load(stream)   
print(Steps)

However it returns only the last output:

{'Step1': {'output': '8192+0 records out', 'input': 'dd if=/dev/urandom of=/dev/null count=8192 bs=8192'}}

How can I get all output values no matter what their number?

Upvotes: 0

Views: 2335

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123450

You cannot do what you want with multiple keys with the same name. YAML associative arrays, like Python dictionaries, must have unique keys, and duplicate keys must be ignored (perhaps producing a warning) or be treated as an error. See the YAML specification:

It is an error for two equal keys to appear in the same mapping node. In such a case the YAML processor may continue, ignoring the second key: value pair and issuing an appropriate warning.

Emphasis mine.

Python dictionaries behave much the same; key-value pairs must be unique. The PyYAML parser handles the above case the same way Python dictionaries do; the last entry wins.

A ticket in the PyYAML tracker has asked for there to be a warning or exception in such cases.

Give your steps unique keys, or use lists for the values instead:

Step1:
    input: dd if=/dev/urandom of=/dev/null count=8192 bs=8192
    output:
        - 8192+0 records in
        - 8192+0 records out

Upvotes: 1

Related Questions