I want to load duplicate keys in Ruamel Yaml. I have a requirement though. I have to use CLOADER given the huge size of my input load file. Hence I need a Loading Hack to load duplicate keys and their values with Cloader. Example:
a.yaml
:
a : {a1:1,a2:2,a3:3}
a : {a1:4,a2:5,a3:6}
b : {b1:1,b2:2}
a.py
:
document = open("a.yaml", "r")
yaml = ruamel.yaml.YAML()
yaml.allow_duplicate_keys = True
data = yaml.load(document)
ruamel.yaml.round_trip_dump(data, sys.stdout)
The output is:
a: {a1: 4, a2: 5, a3: 6}
b: {b1: 1, b2: 2}
The first 'a' row is lost. I tried this in PyYAML with the help from Getting duplicate keys in YAML using Python but issue is: The loading is too slow for a file with 40-50 thousand lines (approx 55 sec). So I read through some forums and got to know "CLOADER" should be used. (I am trying to get CLOADER for pyyaml).
But I want this piece of code to work in ruamel_yaml (given flexibility which I might need later) satisfying two main purposes:
LOAD duplicate keys and their values too. Do not ignore them
TIME: Load should not be slow
How can I solve this?