str.format fails with JSON?

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Tue Feb 21 09:50:19 EST 2017


carlopires at gmail.com writes:

> Hi,
>
> When I run this piece of code:
>
> 'From {"value": 1}, value={value}'.format(value=1)
>
> Python complains about the missing "value" parameter (2.7.12 and
> 3.6.x):

Perhaps you know this, but just to be sure, and for the benefit of any
reader who doesn't: double the braces in the format string when you
don't mean them to be interpreted as a parameter.

> But according to the format string syntax
> (https://docs.python.org/2/library/string.html):

[- -]

> So according to the specification, {value} should be recognized as a
> valid format string identifier and {"value"} should be ignored.
>
> Python seems to not follow the specification in the documentation.
> Anything inside the keys is accepted as identifier.

I think raising an error is more helpful than ignoring it. I think.



More information about the Python-list mailing list