2016-02-05 219 views
0

我想從Python腳本運行我的gnuplot命令。我遇到了一個建議,我可以將所有gnuplot命令保存在一個文件中(格式代碼用於存放我需要的變量),然後用Python讀取該文件並對其進行格式化。但是,我找不到任何示例來了解它是如何完成的。有人可以幫我嗎?從Python腳本讀取gnuplot文件

PS:我正在使用gnuplot版本4.2,我已經嘗試使用-e管道,但由於某種原因它不起作用。 PPS:我寧願不使用的Gnuplot-PY包

+0

可能d重複的http://stackoverflow.com/questions/13204764/python-gnuplot-read-from-file – gariepy

+0

我用這個[包](http://gnuplot-py.sourceforge.net/)。這是舊的,但它運作良好。 –

+0

我不想使用Gnuplot-py軟件包。有其他方法嗎? – user3262537

回答

1

一個簡單的例子:開始與格式代碼的文本文件,

The {adjective1} {adjective2} {mammal1} has escaped! 

然後在Python可以替代值,就像這樣:

# load the template 
with open("template.txt") as inf: 
    template = inf.read() 

# substitute values 
result = template.format(
    adjective1 = "lazy", 
    adjective2 = "brown", 
    mammal1 = "bear" 
) 

# save the result 
with open("plot.dem", "w") as outf: 
    outf.write(result) 

plot.dem文件現在包含

The lazy brown bear has escaped!