Given a template/formatting string "{foo}_{bar}"
, how can I programmatically extract the required formatting keys ["foo", "bar"]
?
I have dicts of parameters for various experiments
[
{"parameters": {"foo": 1, "bar": 2}, "format": `"{foo}_{bar}"`},
{"parameters": {"biz": 3}, "format": "{biz}_{baz}"}
]
As you can see, the second parameter set is missing key baz
. So when I do something like
"{biz}_{baz}".format(**parameters)
, it raises a KeyError, because baz
is missing.
I want to replace all missing parmaters with NR
, and fill all available parameters with their values.
The output is then:
[
{"parameters": {"foo": 1, "bar": 2}, "format": `"{foo}_{bar}"`, "formatted": "1_2"},
{"parameters": {"biz": 3}, "format": "{biz}_{baz}", "formatted": "3_NR"}
]
For context: I have 100+ strings, with no consistency between the expected parameters required for that string.