对命名占位符使用“格式”,但不提供和替换所有占位符?

问题描述 投票:0回答:0

我有一个带有命名占位符的字符串,稍后将替换,但是我想分两步替换它们,最初提供一些,然后稍后在代码中提供剩余的。

例如:

a = '{arg1}{arg2}'
a = a.format(arg1='hello')
>>> a = 'hello{arg2}' # this is what I want it to produce in the first replacement
... more code ...
a = a.format(arg2=' world')
>>> a = 'hello world'

但是,第一次调用

format
会产生一个
KeyError
因为我没有提供
arg2
.

有什么好的方法可以做到这一点吗?

python-2.7
© www.soinside.com 2019 - 2024. All rights reserved.