题
我怎么会使用的子流程模块中的蟒蛇开始的命令行的实例枫饲料和返回的输出,主要代码?例如,我想:
X = '1+1;'
print MAPLE(X)
返回的价值"2".
我见过的最好的是一个圣人的包装的枫的命令,但我会喜欢没有安装和使用的开销圣人我的目的。
解决方案 2
使用从亚历克斯·马尔泰利尖端(谢谢!),我曾与一个明确的回答我的问题上来。在这里发帖,希望其他人可能觉得有用的:
import pexpect
MW = "/usr/local/maple12/bin/maple -tu"
X = '1+1;'
child = pexpect.spawn(MW)
child.expect('#--')
child.sendline(X)
child.expect('#--')
out = child.before
out = out[out.find(';')+1:].strip()
out = ''.join(out.split('\r\n'))
print out
如MAPLE认为有必要长输出分手到许多行,需要的输出的解析。该方法具有保持连接开放MAPLE用于将来计算的优点。
其他提示
下面是如何执行交互式IO与一个命令行程序的例子。我用类似基于所述ispell
命令行实用程序来建立一个拼写检查的内容:
f = popen2.Popen3("ispell -a")
f.fromchild.readline() #skip the credit line
for word in words:
f.tochild.write(word+'\n') #send a word to ispell
f.tochild.flush()
line = f.fromchild.readline() #get the result line
f.fromchild.readline() #skip the empty line after the result
#do something useful with the output:
status = parse_status(line)
suggestions = parse_suggestions(line)
#etc..
这个唯一的问题是,它是非常脆弱和试错的过程,以确保你不发送任何错误的输入和处理所有的不同输出的程序可能会产生。
不隶属于 StackOverflow