4
我遇到了pexpect
問題。我試圖從tralics
讀取乳膠方程併發出MATHML表示,像這樣搶輸出:pexpect捕獲輸出
1 ~/ % tralics --interactivemath
This is tralics 2.14.5, a LaTeX to XML translator, running on tlocal
Copyright INRIA/MIAOU/APICS/MARELLE 2002-2012, Jos\'e Grimm
Licensed under the CeCILL Free Software Licensing Agreement
Starting translation of file texput.tex.
No configuration file.
> $x+y=z$
<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi> <mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>
>
所以我嘗試使用Pexpect的獲得公式:
import pexpect
c = pexpect.spawn('tralics --interactivemath')
c.expect('>')
c.sendline('$x+y=z$')
s = c.read_nonblocking(size=2000)
print s
輸出有公式,但在開始的原始輸入和一些控制字符結尾:
"x+y=z$\r\n<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi><mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>\r\n\r> \x1b[K"
我可以清潔輸出字符串,但我一定是失去了一些東西BA SIC。有沒有更簡潔的方法來獲得MathML?
謝謝!爲了完整性,我做了'c.expect(「」)',然後用'print c.match.group()'得到結果。爲了準備下一個公式,我會'c.expect('>')並重復。 –
Tim
我很高興它幫助:) –