Skip to content
Advertisement

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

JavaScript

I am able to read this correctly in Perl using YAML but not in python using YAML. It fails with the error:

expected a single document in the stream

Program:

JavaScript

Error:

JavaScript

Advertisement

Answer

The yaml documents are separated by ---, and if any stream (e.g. a file) contains more than one document then you should use the yaml.load_all function rather than yaml.load. The code:

JavaScript

results in for the input file as provided in the question:

JavaScript
Advertisement