Format: KeyError when using curly brackets in strings

后端 未结 3 2079
逝去的感伤
逝去的感伤 2021-01-04 08:37

I\'m running the following code:

asset = {}
asset[\'abc\'] = \'def\'
print type(asset)
print asset[\'abc\']
query = \'{\"abc\": \"{abc}\"}\'.format(abc=asset         


        
3条回答
  •  醉梦人生
    2021-01-04 09:09

    wrap the outer braces in braces:

    query = '{{"abc": "{abc}"}}'.format(abc=asset['abc'])
    print query
    {"abc": "def"}
    

提交回复
热议问题