Following code snippet:
import yaml
import collections
def hasher():
return collections.defaultdict(hasher)
data = hasher()
data['this']['is']['me'] = 'test'
print yaml.dump(data)
This returns:
!!python/object/apply:collections.defaultdict
args: [&id001 !!python/name:__main__.hasher '']
dictitems:
this: !!python/object/apply:collections.defaultdict
args: [*id001]
dictitems:
is: !!python/object/apply:collections.defaultdict
args: [*id001]
dictitems: {me: test}
How would I remove:
!!python/object/apply:collections.defaultdict
[*id001]
End goal is:
this:
is:
me: "test"
Any help appreciated!
You need to register a representer with the yaml
module:
from yaml.representer import Representer
yaml.add_representer(collections.defaultdict, Representer.represent_dict)
Now yaml.dump()
will treat defaultdict
objects as though they were dict
objects:
>>> print yaml.dump(data)
this:
is: {me: test}
>>> print yaml.dump(data, default_flow_style=False)
this:
is:
me: test
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