【发布时间】:2015-04-26 00:25:14
【问题描述】:
对此不知所措。我收到一个键错误,但我不知道为什么引用的键看起来像在字典中。
有什么帮助吗?
TEMPLATE = "{ticker:6s}:{shares:3d} x {price:8.2f} = {value:8.2f}"
report = []
stock = {'ticker': 'AAPL', 'price': 128.75, 'value': 2575.0, 'shares': 20}
report.append(TEMPLATE.format(stock))
这是我得到的错误:
report.append(TEMPLATE.format(stock))
KeyError: 'ticker'
【问题讨论】:
标签: python string python-3.x dictionary formatting