Python 无法格式化字符串

Python 无法格式化字符串,python,python-2.7,formatting,string-formatting,Python,Python 2.7,Formatting,String Formatting,我正在尝试格式化文本。下面是我的代码 str = """ def services = {0} try { // try code block } finally { // finally code block } """.format(json.dumps(services)) 但我得到以下错误 Key ' // try code block ' has no corresponding argument 任何帮助都将不胜感激

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

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 block her
}}