具有命名占位符替换的多行字符串格式

时间:2018-09-10 20:03:09

标签: python formatting multiline

我正在通过使用带有命名占位符的格式来进行多行变量替换

port = 2000
replicaset = 'test'

print('''
  mongo --port {port} --eval '
    rs.initiate(
       {
          _id: "{replicaset}",
          version: 1,
          members: [
             { _id: 0, host : "localhost:{port}", priority: 1, votes: 1 },
          ]
       }
    )
  '
  '''.format(replicaset=replicaset, port=port))

我得到的错误如下

21:55 user@machine [machine-tag] ~$ python mongodb-setup.py
Traceback (most recent call last):
  File "mongodb-setup.py", line 59, in <module>
    '''.format(replicaset=replicaset, port=port))
KeyError: '\n        _id'

0 个答案:

没有答案