以下两行有什么问题

时间:2015-12-01 12:40:17

标签: python

我正在尝试编写一段python代码,它将编写一段CMake代码......

但是当我进入下一阶段时:

def_desc = "blaa"
s = "    FILE(WRITE ${CONFIG_H} \"/* {0} */\\n\")\n".format(def_desc)
然后python对我大吼:

Traceback (most recent call last):
  File "/home/ferencd/tmp/blaa.py", line 2, in <module>
    s = "    FILE(WRITE ${CONFIG_H} \"/* {0} */\\n\")\n".format(def_desc)
KeyError: 'CONFIG_H'
[Finished in 0.0s with exit code 1]

据我所知,解释器认为{CONFIG_H}应该是format参数列表中的一个参数......但不,我真的很想将其打印成out vec4 passColor; // passed out to the frag shader out vec2 passTexCoord; // passed out to the frag shader void main() { // gl_Poisition is still in the core profile gl_Position = projectionMatrix * viewMatrix * modelMatrix * inPosition; // need to be passed passColor = inColor; passTexCoord = inTexCoord; } 输出......原样。

我该如何处理这种情况?

2 个答案:

答案 0 :(得分:9)

如果不使用格式变量,则需要转义括号“}”。

def_desc = "blaa"
s = "    FILE(WRITE ${{CONFIG_H}} \"/* {0} */\\n\")\n".format(def_desc)

答案 1 :(得分:5)

你需要使用双括号:

s = "    FILE(WRITE ${{CONFIG_H}} \"/* {0} */\\n\")\n".format(def_desc)

使用模板库这样的东西要容易得多,比如jinja或mako。