pyyaml - python ordereddict or OrderedDict? -
i'm confused right now. i'm using pyyaml editing yaml files.
data = yaml.load_all(open('testingyaml.yaml'),loader=yaml.roundtriploader)
my testingyaml.yaml file contains following content:
spring: profiles: dev datasource: url: jdbc:postgresql://127.0.0.1:5432/nfvgrid dns: enable: false cassandra: host: 192.168.7.151
when print data prints following:
ordereddict([('spring', ordereddict([('profiles', 'dev'), ('datasource', ordereddict([('url', 'jdbc:postgresql://127.0.0.1:5432/nfvgrid')]))])), ('dns', ordereddict([('enable', false)])), ('cassandra', ordereddict([('host', '192.168.7.151')]))])
i further want perform operations on ordered dictionary python throws error nameerror: name 'ordereddict' not defined
it's ordereddict
somehow pyyaml returns ordereddict. how should solve issue?
you not using pyyaml
stated, not have roundtriploader
. instead using ruamel.yaml
.
this alternative uses own ruamel.ordereddict
on python 2 installations, , native collections.ordereddict
on python 3. alternative supports operations ordereddict there no reason cannot manipulate it. see documentation.
however, if wish convert 1 other, like:
x = collections.ordereddict(some_ruamel_ordered_dict.items())
Comments
Post a Comment