无法格式化字符串

Unable to format a string

我正在尝试格式化文本。下面是我的代码。

str = """
    def services = {0}
    try {
        // try code block
    } finally {
        // finally code block
    }
""".format(json.dumps(services))

但我收到以下错误。

Key ' // try code block ' has no corresponding argument

如有任何帮助,我们将不胜感激。

您需要在字符串的 try catch 部分使用双花括号,因为现在格式正在寻找额外的参数。尝试 try {{ }}.. 这样格式才能正常工作。

试试{{}}。喜欢

try {{
   // try block her
}}