- I have a string and using ruamel to load the string and dump the file output in yaml format.
- The string contains arrays of same value.
- If its of same value it misses those value but if there is different values then it prints those values.
Code:
import sys
import json
import ruamel.yaml
import re
dit="{p_d: {p: a0, nb: 0, be: {ar: {1, 1, 1, 1}}, bb: {tt: {dt: {10, 10}, vl: {0}, rl: {0}, sf: {10, 20}, ef: {10, 20}}}}}"
yaml_str=dit
print(yaml_str)
dict_yaml_str = yaml_str.split('\n')
print('#### full block style')
yaml = ruamel.yaml.YAML(typ='safe') #
yaml.default_flow_style = False
yaml.allow_duplicate_keys = True
data = ""
fileo = open("yamloutput.yaml", "w")
for dys in dict_yaml_str:
data = yaml.load(dys)
print("data: {}".format(data))
yaml.dump(data, fileo)
fileo.close()
Output:
p_d:
bb:
tt:
dt:
10: null
ef:
10: null
20: null
rl:
0: null
sf:
10: null
20: null
vl:
0: null
be:
ar:
1: null
nb: 0
p: a0
Expected Output:
p_d:
bb:
tt:
dt:
10: null
10: null
ef:
10: null
20: null
rl:
0: null
sf:
10: null
20: null
vl:
0: null
be:
ar:
1: null
1: null
1: null
1: null
nb: 0
p: a0
Is it some config know from yaml that I am missing ? Please share in your inputs.
It generally helps to find a problem if you minimize the code that reproduces it (i.e. not import
jsonandre, not split a string on newlines that doesn't have a newline, minimize input):You should never have to use
yaml.allow_duplicate_keys, as it is only to allow to mimic faulty behaviour by PyYAML. The fault lies in the fact that YAML doesn't allow duplicate keys and PyYAML does collate these silently, selecting some value (IIRC the last, unless the merge key is involved).If you leave out the
allow_duplicate_keys, and minimize your input to reproduce theDuplicateKeyError, you'll see that the offending key is1:and that the value associated with
1is None. That is because{1, 1, 1, 1}is loaded as if you write{1: null, 1: null, 1: null, 1: null}If you use
.allow_duplicate_keys, you don't get a Python dict with duplicate keys (what you seem to assume), you get a normal Python dict (which doens't allow for duplicate keys) with the value for that duplicate key set to the first value encountered:which gives:
So your expectations that the above dumps as a dictionary with multiple items is incorrect.