Reputation: 3514
msg = '{"event":"addChannel","channel":"ok_sub_spot{currency}_{market}_trades"}'
print msg.format(**{'currency': 'usd', 'market': 'btc'})
I want to format this but I get an error.
Traceback (most recent call last):
File "/Users/wyx/bitcoin_workspace/fibo/tests/t_ws.py", line 21, in <module>
print msg.format(**{'currency': 'usd', 'market': 'btc'})
KeyError: '"event"'
I even don't know why I get this error.
Upvotes: 1
Views: 62
Reputation: 23176
In a format string {
and }
are reserved characters indicating a group you wish to replace. If you actually want either of those characters in the string, you need to double them, as {{
and }}
, like so:
>>> msg = '{{"event":"addChannel","channel":"ok_sub_spot{currency}_{market}_trades"}}'
>>> print msg.format(**{'currency': 'usd', 'market': 'btc'})
{"event":"addChannel","channel":"ok_sub_spotusd_btc_trades"}
Upvotes: 3
Reputation: 9177
You may use
msg = "{"+'{"event":"addChannel","channel":"ok_sub_spot{currency}_{market}_trades"}'+"}"
Otherwise it will be interpreted "event"
as a key.
Upvotes: 1