【发布时间】:2018-12-01 23:38:19
【问题描述】:
我正在尝试使用字典来促进一些报告。字典包含一些带有格式变量的模板;我想填充这些。
这是我正在努力实现的一个独立的极简主义示例:
ISSUES = {
'BIG_ISSUE': {
'code': 1,
'title': 'Something interesting',
'detail': 'This is the affected domain {domain}'
},
'OTHER_ISSUE': {
'code': 2,
'title': 'Some other issue',
'detail': 'Blah.'
}
}
domain = 'foo.bar'
issue = ISSUES['BIG_ISSUE']
issue['detail'].format(domain=domain)
print(issue)
这是上面的输出:
{'code': 1, 'title': 'Something interesting', 'detail': 'This is the affected domain {domain}'}
请注意,上面的 {domain} 未在输出中格式化。
这是我正在寻找的预期结果:
{'code': 1, 'title': 'Something interesting', 'detail': 'This is the affected domain foo.bar'}
我相信这是因为字符串是不可变的?我尝试按照 SO 上的一些示例并尝试使用 dict() 和 import copy; copy.deepcopy() 但这给了我相同的结果。
【问题讨论】:
标签: python-3.x dictionary copy