I\'m running the following code:
asset = {}
asset[\'abc\'] = \'def\'
print type(asset)
print asset[\'abc\']
query = \'{\"abc\": \"{abc}\"}\'.format(abc=asset
To insert a literal brace, double it up:
query = '{{"abc": "{abc}"}}'.format(abc=asset['abc'])
(This is documented here, but not highlighted particularly obviously).
The topmost curly braces are interpreted as a placeholder key inside your string, thus you get the KeyError
. You need to escape them like this:
asset = {}
asset['abc'] = 'def'
query = '{{"abc": "{abc}"}}'.format(**asset)
And then:
>>> print query
{"abc": "def"}
wrap the outer braces in braces:
query = '{{"abc": "{abc}"}}'.format(abc=asset['abc'])
print query
{"abc": "def"}