我有一个正在尝试创建的RRD数据库,并且我编写了几个循环来构建该命令,它工作良好,并产生预期的输出。当我实际尝试运行rrdtool时,问题就出现了。Python中的create命令。这个循环产生一个字符串,我们将其命名为rrdStr
,包含以下内容:
'"0x04.rrd", "--step","5",
"DS:TEMP:GAUGE:10:0:100",
"DS:HUMIDITY:GAUGE:10:0:100",
"DS:LIGHT:GAUGE:10:0:1024",
"DS:POT:GAUGE:10:0:1024",
"RRA:AVERAGE:0.5:1:17142",
"RRA:MIN:0.5:1:17142",
"RRA:MAX:0.5:1:17142",
"RRA:AVERAGE:0.5:12:7200",
"RRA:MIN:0.5:12:7200",
"RRA:MAX:0.5:12:7200",
"RRA:AVERAGE:0.5:60:8640",
"RRA:MIN:0.5:60:8640",
"RRA:MAX:0.5:60:8640",
"RRA:AVERAGE:0.5:360:17532",
"RRA:MIN:0.5:360:17532",
"RRA:MAX:0.5:360:17532"'
据我所知,所有的引号和换行符都被正确转义了。
如果我像这样将字符串变量传递给rrdtool.create
: rrdtool.create(rrdStr)
,我得到输出
rrdtool.error: you must define at least one Round Robin Archive
但是,如果我将上述输出直接复制并粘贴到rrdtool.create()
函数中,它就会像预期的那样工作,并创建数据库。
知道是什么原因导致的,如何解决它?
我终于想通了。您必须创建一个列表,而不是创建一个包含正确格式的所有命令的长字符串,rrd命令的每一行都在命令生成时添加到列表中。
上面的命令变成:
rrdStr = ["0x04.rrd", "--step","5"]
rrdStr += ["DS:TEMP:GAUGE:10:0:100"]
rrdStr += ["DS:HUMIDITY:GAUGE:10:0:100"]
rrdStr += ["DS:LIGHT:GAUGE:10:0:1024"]
rrdStr += ["DS:POT:GAUGE:10:0:1024"]
rrdStr += ["RRA:AVERAGE:0.5:1:17142"]
rrdStr += ["RRA:MIN:0.5:1:17142"]
rrdStr += ["RRA:MAX:0.5:1:17142"]
rrdStr += ["RRA:AVERAGE:0.5:12:7200"]
rrdStr += ["RRA:MIN:0.5:12:7200"]
rrdStr += ["RRA:MAX:0.5:12:7200"]
rrdStr += ["RRA:AVERAGE:0.5:60:8640"]
rrdStr += ["RRA:MIN:0.5:60:8640"]
rrdStr += ["RRA:MAX:0.5:60:8640"]
rrdStr += ["RRA:AVERAGE:0.5:360:17532"]
rrdStr += ["RRA:MIN:0.5:360:17532"]
rrdStr += ["RRA:MAX:0.5:360:17532"]
您可以将列表作为唯一的命令传递rrdtool.create(rrdStr)
之前失败的相同命令将成功运行
在python2.6上使用rrdtool.create(*rrdStr)