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 reads:
yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/tuple'
What needs to be done?
At least according to the PyYAML documentation:
The list, as you can see in the source, is somewhat more extensive but does not include
tag:yaml.org,2002:python/tuple
.It appears that if you are generating a
!!python/tuple
type in your YAML file, you are usingdump()
as opposed tosafe_dump()
. If that's the case, you should probably switch to usingload()
in place ofsafe_load()
, as files created bydump()
are not guaranteed to be loadable bysafe_load()
. (See the description ofsafe_dump()
).In pyyaml, the SafeLoader does not include a loader for the python native types, only the types defined in the yaml spec. You can see the types for the
SafeLoader
and theLoader
here in the interaction sample below.You can define a new Loader class that adds in the python tuple, but not other types, so it should still be pretty safe:
resulting in:
See below for the constructors associated with the SafeLoader and the Loader classes.