PyYAML parse into arbitary object
Posted
by Philip Fourie
on Stack Overflow
See other posts from Stack Overflow
or by Philip Fourie
Published on 2010-03-14T16:58:19Z
Indexed on
2010/03/14
17:05 UTC
Read the original article
Hit count: 374
I have the following Python 2.6 program and YAML definition (using PyYAML):
import yaml
x = yaml.load(
"""
product:
name : 'Product X'
sku : 123
features :
- size : '10x30cm'
weight : '10kg'
"""
)
print type(x)
print x
Which results in the following output:
<type 'dict'>
{'product': {'sku': 123, 'name': 'Product X', 'features': [{'weight': '10kg', 'size': '10x30cm'}]}}
It is possible to create a strongly typed object from x
?
I would like to the following:
print x.features(0).size
I am aware that it is possible to create and instance from an existent class, but that is not what I want for this particular scenario.
© Stack Overflow or respective owner