如何在json格式的字符串中使用str.format? [英] How to use str.format inside a string of json format?

查看:128
本文介绍了如何在json格式的字符串中使用str.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 {}. 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大括号:

So if you want to use .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 :

There's a better way to do that with string.Template:

>>> from string import Template
>>> t = Template('{"fvAp": {"attributes": {"name": "${name}"}}')
>>> t.substitute(name='StackOverflow')
'{"fvAp": {"attributes": {"name": "StackOverflow"}}'

尽管我建议完全放弃以这种方式生成配置并使用工厂函数和 json.dumps 代替:

Though I suggest abandoning the idea of generating configs this way altogether and using a factory function and 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"}}}'

这篇关于如何在json格式的字符串中使用str.format?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆