简体   繁体   中英

Python string.format KeyError

This question has been answered before, but my string doesn't have any extra curly brackets that would mess up the formatting, so at the moment I'm completely clueless as to why the error

Error is KeyError : content

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)

you could do it line by line using a dictionary and passing the dict as keyword arguments using **

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

html = html.format(**d)

你需要一次性填充这些值:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM