What's wrong with the following two lines
本问题已经有最佳答案,请猛点这里访问。
我正在尝试编写一段python代码,它将编写一段cmake代码…
但当我进入以下阶段时:
1 2 3 4 | def_desc ="blaa" s =" FILE(WRITE ${CONFIG_H} "/* {0} */\ ") ".format(def_desc) |
然后Python冲我大叫:
1 2 3 4 5 6 7 | Traceback (most recent call last): File"/home/ferencd/tmp/blaa.py", line 2, in <module> s =" FILE(WRITE ${CONFIG_H} "/* {0} */\ ") ".format(def_desc) KeyError: 'CONFIG_H' [Finished in 0.0s with exit code 1] |
我知道,不知怎么地,解释器认为
我该如何处理这种情况?
如果不用于格式变量,则需要转义括号""。
1 2 3 4 | def_desc ="blaa" s =" FILE(WRITE ${{CONFIG_H}} "/* {0} */\ ") ".format(def_desc) |
您需要使用双大括号:
1 2 3 | s =" FILE(WRITE ${{CONFIG_H}} "/* {0} */\ ") ".format(def_desc) |
不过,使用模板库处理类似于Jinja或Mako的东西要容易得多。