Reading YAML file with Python results in yaml.composer.ComposerError: expected a single document in the stream
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:
import yaml
stream = open("test", "r")
docs = yaml.load_all(stream)
for doc in docs:
for k,v in doc.items():
print k, "->", v
print "\n",
results in for the input file as provided in the question:
request -> 341570
level_1 -> test
level_2 -> NetApp, SOFS, ZFS Creation
request -> 341569
level_1 -> test
level_2 -> NetApp, SOFS, ZFS Creation
request -> 341568
level_1 -> test
level_2 -> NetApp, SOFS, ZFS Creation