如何在json格式的字符串中使用str.format? [英] How to use str.format inside a string of json format?
问题描述
Python 3.5版
Python Version 3.5
我正在尝试进行API调用以使用json作为格式配置设备.某些json会根据所需的命名而有所不同,因此我需要在字符串中调用一个变量.我可以使用旧样式%s... % (variable)
完成此操作,但不能使用新样式{}... .format(variable)
.
I'm trying to make an API call to configure a device using json as the format. Some of the json will vary depending on the desired naming, so I need to call a variable in the string. I am able to accomplish this using the old style %s... % (variable)
, but not with the new style {}... .format(variable)
.
失败的EX:
(Testing with {"fvAp":{"attributes":{"name":(variable)}}})
a = "\"app-name\""
app_config = ''' { "fvAp": { "attributes": { "name": {} }, "children": [ { "fvAEPg": { "attributes": { "name": "app" }, "children": [ { "fvRsBd": { "attributes": { "tnFvBDName": "default" }, } } ] } }, { "fvAEPg": { "attributes": { "name": "db" }, "children": [ { "fvRsBd": { "attributes": { "tnFvBDName": "default" }, } } ] } } ] } } '''.format(a)
print(app_config)
跟踪(最近一次通话最近一次):文件"C:/...,第49行,位于'''.format('a')KeyError:'\ n"fvAp"'
Traceback (most recent call last): File "C:/..., line 49, in '''.format('a') KeyError: '\n "fvAp"'
正在使用EX:
a = "\"app-name\""
app_config = ''' { "fvAp": { "attributes": { "name": %s }, "children": [ { "fvAEPg": { "attributes": { "name": "app" }, "children": [ { "fvRsBd": { "attributes": { "tnFvBDName": "default" }, } } ] } }, { "fvAEPg": { "attributes": { "name": "db" }, "children": [ { "fvRsBd": { "attributes": { "tnFvBDName": "default" }, } } ] } } ] } } ''' % a
print(app_config)
如何使用str.format
方法使它正常工作?
How do I get this to work using str.format
method?
推荐答案
格式字符串包含用花括号 Format strings contain "replacement fields" surrounded by curly braces 因此,如果要使用 So if you want to use 那看起来真的很糟糕. 还有一种更好的方法,可以使用 There's a better way to do that with 尽管我建议完全放弃以这种方式生成配置并使用工厂函数和 Though I suggest abandoning the idea of generating configs this way altogether and using a factory function and 这篇关于如何在json格式的字符串中使用str.format?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!{}
包围的替换字段".花括号中不包含的所有内容均视为文字文本,该文本原样复制到输出中.如果需要在文字文本中包含大括号字符,可以通过将{{
和}}
加倍来进行转义.
{}
. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{
and }}
..format
方法,则需要转义模板字符串中的所有JSON大括号:.format
method, you need to escape all JSON curly braces in your template string:>>> '{{"fvAp": {{"attributes": {{"name": {}}}}}}}'.format('"app-name"')
'{"fvAp": {"attributes": {"name": "app-name"}}}'
string.Template
:string.Template
:>>> from string import Template
>>> t = Template('{"fvAp": {"attributes": {"name": "${name}"}}')
>>> t.substitute(name='StackOverflow')
'{"fvAp": {"attributes": {"name": "StackOverflow"}}'
json.dumps
代替:json.dumps
instead:>>> import json
>>> def make_config(name):
... return {'fvAp': {'attributes': {'name': name}}}
>>> app_config = make_config('StackOverflow')
>>> json.dumps(app_config)
'{"fvAp": {"attributes": {"name": "StackOverflow"}}}'