I try to format a string with curly brackets like this:
dct_mapper = dict(a=1)
s = '{key1: value2, key1: value2}; attrib1={a}; attrib2={b}'
s.format(**dct_mapper)
Results in:
# KeyError: 'key1'
Expected:
# '{key1: value2, key1: value2}; attrib1=1; attrib2={b}'
An additional constrainment is that I need to use .format_map()
later on the output again, this screws up solutions which use .format() because the out curly brackets will disappear.
I tried defaultdict from the collections package and also into .format_map(), then plaed around with regex trying to replace the brackets with additional ones, which feels less like a solution and more like a hack and also doesn't work if you have multiple repeating brackets in the string to begin with.
It is not a json string, because then I could have used the json library to map the values.
Does anyone have an idea how to solve this?
I currently consider using a loop and str.replace('{a}', 1)
but this feels clunky as well.