dict = {
i: [{'cam_R_m2c': cam_R_m2c},
{'cam_T_m2c': cam_T_m2c},
{'obj_bb': [0, 0, 0, 0]},
{'obj_id': 0}
]
}
# list = [{'cam_R_m2c':cam_R_m2c},{'cam_T_m2c':cam_T_m2c},{'obj_bb': [0,0,0,0]},{'obj_id': 0}]
with open('./Data_collection/gt.yml','a+') as f:
yaml.dump(dict,f)
I get a yaml file like this
0:
- cam_R_m2c:
- 0.999988853931427
- -0.0014960498083382845
- -0.004474445711821318
- 0.0014844260876998305
- 0.9999955296516418
- -0.0025999906938523054
- 0.004478315357118845
- 0.0025933198630809784
- 0.9999865889549255
- cam_T_m2c:
- 0.014818123541772366
- 9.016657713800669e-05
- 0.00015443217125721276
- obj_bb:
- 0
- 0
- 0
- 0
- obj_id: 0
However I want to get this format:
0:
- cam_R_m2c: [0.09630630, 0.99404401, 0.05100790, 0.57332098, -0.01350810, -0.81922001, -0.81365103, 0.10814000, -0.57120699]
cam_t_m2c: [-105.35775150, -117.52119142, 1014.87701320]
obj_bb: [244, 150, 44, 58]
obj_id: 1
What should I do?
There are a few problems with your code:
wb
to open a file or preferably pass in a pathlib.Path
instance, more concise and it opens the file in the correct waydict
is a reserved word in Python, you shouldn't mask it by using it as variable name.yaml
since at least September 2006. YML format looks completely different as it an XML format.The Python list like output for sequences in YAML is called flow-style (the other format is block style, which is the default for output in ruamel.yaml
)
Then there is the issue that the output that you get is not semantically equivalent to the output that you want, which you can check if you load that data. In the output that you get (and in your definition of the variable dict
), the value for the key 0
is a sequence of four items, each item being a mapping with a single key (in Python terms a list of four elements, each element being a dict). In the output that you want the value for that key is a sequence with a single element, and that element is a mapping with four keys.
If the requested output format is correct the solution is simple, you have to change the data structure:
from pathlib import Path
import ruamel.yaml
i = 0
cam_R_m2c = [0.09630630, 0.99404401, 0.05100790, 0.57332098, -0.01350810, -0.81922001, -0.81365103, 0.10814000, -0.57120699]
cam_T_m2c = [-105.35775150, -117.52119142, 1014.87701320]
data = {
i: [{'cam_R_m2c': cam_R_m2c, # curly braces removed on this and the following lines
'cam_T_m2c': cam_T_m2c,
'obj_bb': [244, 150, 44, 50],
'obj_id': 1}, # I recommend getting in the habit of also adding a comman after the last key/value pair or element
]
}
out_file = Path('gt.yaml')
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = None # this makes the leaf nodes flow style
yaml.width = 2048 # to prevent line wrapping
yaml.dump(data, out_file)
print(out_file.read_text(), end='')
which gives:
0:
- cam_R_m2c: [0.0963063, 0.99404401, 0.0510079, 0.57332098, -0.0135081, -0.81922001, -0.81365103, 0.10814, -0.57120699]
cam_T_m2c: [-105.3577515, -117.52119142, 1014.8770132]
obj_bb: [244, 150, 44, 50]
obj_id: 1
If your input is correct and you actually want:
0:
- cam_R_m2c: [0.09630630, 0.99404401, 0.05100790, 0.57332098, -0.01350810, -0.81922001, -0.81365103, 0.10814000, -0.57120699]
- cam_t_m2c: [-105.35775150, -117.52119142, 1014.87701320]
- obj_bb: [244, 150, 44, 58]
- obj_id: 1
it doesn't suffice to set yaml.default_flow_style
:
import sys
data = {
i: [{'cam_R_m2c': cam_R_m2c},
{'cam_T_m2c': cam_T_m2c},
{'obj_bb': [244, 150, 44, 50]},
{'obj_id': 1},
]
}
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = None
yaml.width = 2048
yaml.dump(data, sys.stdout)
which gives:
0:
- cam_R_m2c: [0.0963063, 0.99404401, 0.0510079, 0.57332098, -0.0135081, -0.81922001, -0.81365103, 0.10814, -0.57120699]
- cam_T_m2c: [-105.3577515, -117.52119142, 1014.8770132]
- obj_bb: [244, 150, 44, 50]
- {obj_id: 1}
because the mapping with key obj_id
is now also a leaf node.
To change an individual dict
to a block style mapping:
def BSD(d):
ret_val = ruamel.yaml.comments.CommentedMap(d)
ret_val.fa.set_block_style()
return ret_val
data = {
i: [{'cam_R_m2c': cam_R_m2c},
{'cam_T_m2c': cam_T_m2c},
{'obj_bb': [244, 150, 44, 50]},
BSD({'obj_id': 1}),
]
}
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = None
yaml.width = 2048
yaml.dump(data, sys.stdout)
which gives:
0:
- cam_R_m2c: [0.0963063, 0.99404401, 0.0510079, 0.57332098, -0.0135081, -0.81922001, -0.81365103, 0.10814, -0.57120699]
- cam_T_m2c: [-105.3577515, -117.52119142, 1014.8770132]
- obj_bb: [244, 150, 44, 50]
- obj_id: 1
You can also leave the default block style and change the individual list
s to dump as flow-style sequences:
def FSL(d):
ret_val = ruamel.yaml.comments.CommentedSeq(d)
ret_val.fa.set_flow_style()
return ret_val
data = {
i: [{'cam_R_m2c': FSL(cam_R_m2c)},
{'cam_T_m2c': FSL(cam_T_m2c)},
{'obj_bb': FSL([244, 150, 44, 50])},
{'obj_id': 1},
]
}
yaml = ruamel.yaml.YAML()
yaml.width = 2048
yaml.dump(data, sys.stdout)
which the same result:
0:
- cam_R_m2c: [0.0963063, 0.99404401, 0.0510079, 0.57332098, -0.0135081, -0.81922001, -0.81365103, 0.10814, -0.57120699]
- cam_T_m2c: [-105.3577515, -117.52119142, 1014.8770132]
- obj_bb: [244, 150, 44, 50]
- obj_id: 1
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments