I am using yaml.safe_load()
but I need to ignore a tag !v2
-- is there a way to do this but still use safe_load()
?
Extending the existing answer to support ignoring all unknown tags.
import yaml
class SafeLoaderIgnoreUnknown(yaml.SafeLoader):
def ignore_unknown(self, node):
return None
SafeLoaderIgnoreUnknown.add_constructor(None, SafeLoaderIgnoreUnknown.ignore_unknown)
root = yaml.load(content, Loader=SafeLoaderIgnoreUnknown)
I figured it out, it's related to How can I add a python tuple to a YAML file using pyYAML?
I just have to do this:
yaml.SafeLoader
add_constructor
to assign !v2
to a custom construction methodyaml.load(..., MyLoaderClass)
instead of yaml.safe_load(...)
and it works.
class V2Loader(yaml.SafeLoader):
def let_v2_through(self, node):
return self.construct_mapping(node)
V2Loader.add_constructor(
u'!v2',
V2Loader.let_v2_through)
....
y = yaml.load(info, Loader=V2Loader)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With