[英]How I format a json of dict
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. 我什至不知道为什么我得到这个错误。
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"}
You may use 您可以使用
msg = "{"+'{"event":"addChannel","channel":"ok_sub_spot{currency}_{market}_trades"}'+"}"
Otherwise it will be interpreted "event"
as a key. 否则,它将被解释为
"event"
作为键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.