I need to create a dictionary string:
param1 = "abc"
param2 = "def"
input_data = """{"param1": {}, "param2": {}}""".format(param1, param2)
But I get this error:
KeyError: '"param1"'
You need to escape (double) characters {
and }
in format strings when they represent themselves (not used for formatting):
param1 = "abc"
param2 = "def"
input_data = """{{"param1": {}, "param2": {}}}""".format(param1, param2)
Your format string is seeing the first {
as the start of a formatter, so you have to escape it by using a double {{
:
input_data = """{{"param1": {}, "param2": {}}}""".format(param1, param2)
try this way.
param1 = "abc", param2 = "def"
input_data = str({"param1": param1, "param2": param2})
Output of input_data
:
"{'param1': 'abc', 'param2': 'def'}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.