In [5]: x = "this string takes two like {one} and {two}"
In [6]: y = x.format(one="one")
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-6-b3c89fbea4d3> in <module>()
----> 1 y = x.format(one="one")
KeyError: 'two'
一个问题我已经与被保存在配置文件中的许多键复合字符串。对于8个不同的查询,它们都使用相同的字符串,但1个密钥是不同的设置。我需要能够替代该文件一键拯救字符串后,如:
"this string takes two like one and {two}"
如何在使用format
一次替换一个关键?谢谢
你至少可以做'y = x.format(one =“one”,two =“{two}”)'但这可能不是最好的方法来处理它... – JohanL
'y = x.replace('{one }','不管')'? – khelwood