avatar

目录
python yaml load

Load yaml to a nested dict structure, and then parse to a class.
below is the code:

python
1
2
3
4
5
6
7
8
9
10
class Struct(object):
def __init__(self, data):
for name, value in data.items():
setattr(self, name, self._wrap(value))

def _wrap(self, value):
if isinstance(value, (tuple, list, set, frozenset)):
return type(value)([self._wrap(v) for v in value])
else:
return Struct(value) if isinstance(value, dict) else value
python
1
2
3
with open(r'c:\Users\uidn4064\Desktop\project_data_analysis\FD69\lab\test.yaml') as stream:
yaml_dict = yaml.load(stream)
struct = Struct(yaml_dict)

评论