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.

7
  • Can you put your full code here ?? Commented Dec 19, 2013 at 4:16
  • 2
    You need to escape the extra {s Commented Dec 19, 2013 at 4:17
  • This is the complete code so far. I just need to adjust 3 strings where I specified. Commented Dec 19, 2013 at 4:19
  • I don't think format can handle the nested braces. You need to the ones that aren't meant for variables. Commented Dec 19, 2013 at 4:19
  • Where is the extra '{' @desiredlogin Commented Dec 19, 2013 at 4:20

5 Answers 5

30

{ 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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank god I found your answer after half day headache!
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}'

Comments

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?

1 Comment

@BurhanKhalid it doesn't require code, this is clearly a fixed json data structure.
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.

Comments

-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("]","}")

2 Comments

Indeed this would just create havoc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.