15

I have a string and I want to use .format function of python to add some variables in it on runtime, This is my string :

'{"auth": {"tenantName": "{Insert String Here}", "passwordCredentials": {"username": "{insert String here}", "password": "{insert String Here}"}}}'

when I use .format like this:

credentials='{"auth": {"tenantName": "{tenant}", "passwordCredentials": {"username": "{admin}", "password": "{password}"}}}'.format(tenant='me',admin='test',password='123')

It gives me the following error:

KeyError: '"auth"'

Any Help? Thanks in Advance.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
mobykhn
  • 221
  • 1
  • 4
  • 14

5 Answers5

29

{ and } are special characters for string formatting, as you clearly are aware, since you are using them for {tenant}, {admin} and {password}. All the other {s and }s need to be escaped by doubling them. Try:

credentials='{{"auth": {{"tenantName": "{tenant}", "passwordCredentials": {{"username": "{admin}", "password": "{password}"}}}}}}'.format(tenant='me',admin='test',password='123')
desired login
  • 1,138
  • 8
  • 15
6

You are using the wrong tool for your trade. You are dealing with json, and you need to use the json library to parse your data and then access your field as a dictionary

>>> import json
>>> data_dict = json.loads(data)
>>> data_dict["auth"]["tenantName"]
u'{Insert String Here}'
Abhijit
  • 62,056
  • 18
  • 131
  • 204
0

The .format function is choking on your extra { } braces. It's looking for those braces as an indication of what to search and replace, so when it sees those braces it thinks it's looking at a key to replace.

You need to escape the braces that are not meant to indicate keys. For .format, that's done by doubling the brace. So your code should look like:

credentials='{{"auth": {{"tenantName": "{tenant}", "passwordCredentials": {{"username": "{admin}", "password": "{password}"}}}}}}'.format(tenant='me',admin='test',password='123')

See the docs: http://docs.python.org/2/library/string.html#formatstrings

Also see this question: How can I print literal curly-brace characters in python string and also use .format on it?

Community
  • 1
  • 1
Ash Eldritch
  • 1,504
  • 10
  • 13
0

Your string begins with {"auth". As soon as the format string parser sees that opening curly brace it thinks that "auth" is the name of a format variable as passed in to .format(). You need to escape any curly braces in your template string with double curly_braces, like {{.

That said, it looks like you're trying to build a JSON string. Just use the json module for that.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
-1

I think the braces might be killing you. If you are using format, it expects things inside { } to be keys. That is, I don't think you can use .format in a string which contains non-formatting `{...}' because it doesn't know how to parse it. However, you could do:

credentials='["auth": ["tenantName": "{tenant}", "passwordCredentials": ["username": "{admin}", "password": "{password}"]]}'.format(tenant='me',admin='test',password='123')

Then do a

credentials.replace("[","{")
credentials.replace("]","}")
Tommy
  • 12,588
  • 14
  • 59
  • 110