Python string.format键错误

Python string.format键错误,python,Python,这个问题以前已经回答过了,但是我的字符串没有任何额外的花括号会弄乱格式,所以目前我完全不知道为什么会出现错误 错误是关键错误:内容 html = """ <table class=\"ui celled compact table\" model=\"{model}\"> {theaders} <tbody> {content} </tbody> </table>

这个问题以前已经回答过了,但是我的字符串没有任何额外的花括号会弄乱格式,所以目前我完全不知道为什么会出现错误

错误是关键错误:内容

html = """
    <table class=\"ui celled compact table\" model=\"{model}\">
        {theaders}
        <tbody>
            {content}
        </tbody>
    </table>
    """
html = html.format(model=model)
html = html.format(content=data)
html = html.format(theaders=theaders)
html=”“”
{theaders}
{content}
"""
html=html.format(model=model)
html=html.format(内容=数据)
html=html.format(theaders=theaders)

您需要一次性填写这些值:

html.format(model=model, content=data, theaders=theaders)

您可以使用字典逐行执行,并使用
**

d=dict()
d['model']=model
d['content']=data
d['theaders']=theaders

html = html.format(**d)

没有办法把它分成几部分吗?@Mojimi不是直接通过。使此功能使用不同的占位符语法。但你为什么需要它?是的,你可以。您需要将
内容
用双括号括起来
theaders
带有四个括号(
{model}…{{{content}}..{{{{{theaders}}}}
)。有点黑客…模板字符串就是答案@Mojimi
html=Template(html).safe_替换(model=model);html=模板(html).安全替换(内容=数据)
。但我真的建议采用@jean-françois fabre的回答:在这个例子中不需要反斜杠。对字符串进行三重引号的好处之一是,任何单个引号字符都不再特殊。我只是在试图找出错误时才这样做的:)