Reputation: 10138
I want to improve YAML syntax to make human readable exchange format.
I have such code:
import yaml
class YamlFileLoader(yaml.Loader):
pass
class YamlFileDumper(yaml.Dumper):
pass
class YAMLPerson(yaml.YAMLObject):
yaml_tag = u'!person'
yaml_flow_style = False
def __init__(self):
self.first_name = u'John'
self.last_name = u'Doe'
y = YAMLPerson()
print yaml.dump(y)
It produces little ugly output:
!person
first_name: !!python/unicode 'John'
last_name: !!python/unicode 'Doe'
How to transform this into nice human readable syntax like that:
person:
first_name: 'John'
last_name: 'Doe'
What minimal code I should add?
Upvotes: 2
Views: 1940
Reputation: 56
Chameleon,
I reached your post through google, as I was also looking for the same. I found the answer and thought of sharing with you.
Use "default_flow_style=False", to make the output human readable. See the sample code.
import yaml
yaml_sample = """
a: 1
b:
c: 3
d: 4
"""
my_yaml = yaml.load(yaml_sample)
print "default style True == \n", yaml.dump(my_yaml)
print "default style False == \n", yaml.dump(my_yaml, default_flow_style=False)
Output,
default style True ==
a: 1
b: {c: 3, d: 4}
default style False ==
a: 1
b:
c: 3
d: 4
Upvotes: 3