I have the following code that interpolates values in strings:
from collections import defaultdict
"This is {foo} {bar}".format_map(defaultdict(str, **{"foo": "a test"}))
>>> 'This is a test '
This interpolates known values and replaces unknown values with an empty string.
But if I write an unbalanced bracket in the string:
"This is {foo} {bar]".format_map(defaultdict(str, **{"foo": "a test"}))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: expected '}' before end of string
Is there a way I can avoid this error and get the following output?
"This is {foo} {bar]".format_map(defaultdict(str, **{"foo": "a test"}))
>>> 'This is a test {bar]'