0

我想使用 pexpect 从 python 中使用 maxima,每当 maxima 启动时,它都会打印一堆这种形式的东西:

$ maxima
Maxima 5.27.0 http://maxima.sourceforge.net
using Lisp SBCL 1.0.57-1.fc17
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1)

我想像这样启动pexpect:

import pexpect 
cmd = 'maxima'
child = pexpect.spawn(cmd)
child.expect (' match all that stuff up to and including (%i1)')
child.sendline ('integrate(sin(x),x)')
chil.expect( match (%i2) i think ; see below sample session  ) 
print child.before 

如何将起始横幅与提示符 (%i1) 匹配?依此类推,随着会话的进行,最大值也会将 (%i1) 的值加一,

(%i1) integrate(sin(x),x);
(%o1)                              - cos(x)
(%i2) integrate(log(x),x);
(%o2)                            x log(x) - x
(%i3)

所以下一个期望是:

child.expect ('match (%i2)')
child.sendline ('integrate(log(x),x)')
child.expect( match (%i3) ) 
print child.before 

我如何匹配(递增)整数?基本上我需要在打印(%o#)时匹配(%i#)。

4

1 回答 1

1

这个正则表达式匹配它:\(%i\d\). 如果您需要匹配(%o#)s 只需在针中替换i为。o

于 2012-12-15T11:27:55.027 回答