I am writing python script which can automate my work of yaml. I will create a yaml strucutre from different csv files. But at the moment I am trying to understand yaml strucutre via examples. I was looking at some yaml tutorials and examples and i came across one problem to address properly
my python code is as follow for the above structure
import sys
import yaml
from collections import OrderedDict
d = {'version': '22-07-2017', 'description': 'energie balance',
'info': {
'principalInvestigator': 'Kalthoff',
'personInCharge': 'Scheer'
},
'dataSources': 'null',
'devices': {
'type': 'HMP',
'info': {
'description': 'temperature and humidity sensor',
'company': 'Vaisala',
'model': 'HMP35A',
},
'measures': {
'quantity': 'T',
'annotations': 'air',
'sensors': {
'number': '001',
'sources': {
'id': 'null',
'frequency': '0.1',
'aggregation': 'AVG',
'field': 'null'
}
}
}
}
}
with open('/home/ali/Desktop/yaml-conf-task/result.yml', 'w') as yaml_file:
yaml.dump(d, yaml_file, default_flow_style=False)
But when I open yaml file it give me un ordered data . i receive this
dataSources: 'null'
description: energie balance
devices:
info:
company: Vaisala
description: temperature and humidity sensor
model: HMP35A
measures:
annotations: air
quantity: T
sensors:
number: '001'
sources:
aggregation: AVG
field: 'null'
frequency: '0.1'
id: 'null'
type: HMP
info:
personInCharge: Scheer
principalInvestigator: Kalthoff
version: 22-07-2017
instead of getting this
version: 21-07-2017
description: energie balance
info:
principalInvestigator: rob
personInCharge: rio
dataSources: null
devices:
- type: TMP
info:
description: temperature and humidity sensor
company: Vio
model: 35A
measures:
- quantity: T
annotation: air
sensors:
- number: 001
sources:
- id: null
frequency: 1
aggregation: AVG
field: null
If someone suggest me how can i maintain the order, i would be grateful. I look over stack overflow, but couldn't solve my problem.