我有什么不明白的吗?
下面的代码运行良好:
from sympy.printing.mathml import print_mathml
s = "x**2 + 8*x + 16"
print_mathml(s)但这会产生一个错误:
from sympy.printing.mathml import mathml
s = "x**2 + 8*x + 16"
print mathml(s)最终,我要做的是将"x**2 + 8*x + 16“转换为presentation MathML以便在web上输出。所以我的计划是对字符串使用mathml()函数,然后通过c2p发送输出,如下所示:
from sympy.printing.mathml import mathml
from sympy.utilities.mathml import c2p
s = "x**2 + 8*x + 16"
print(c2p(mathml(s))但是如上所述,mathml()函数抛出了一个错误。
发布于 2014-12-27 01:20:47
我还没有足够的名誉点来评论,所以我转而回答。
根据Sympy文档的.sympy.printing.mathml的mathml函数需要一个表达式。但是是字符串格式吗?
http://docs.sympy.org/dev/modules/printing.html#sympy.printing.mathml.mathml
代码部分:
def mathml(expr, **settings):
"""Returns the MathML representation of expr"""
return MathMLPrinter(settings).doprint(expr)-
def doprint(self, expr):
"""
Prints the expression as MathML.
"""
mathML = Printer._print(self, expr)
unistr = mathML.toxml()
xmlbstr = unistr.encode('ascii', 'xmlcharrefreplace')
res = xmlbstr.decode()
return res你得到了哪个错误?
你拿到这个了吗:
[....]
return MathMLPrinter(settings).doprint(expr) File "C:\Python27\lib\site-packages\sympy\printing\mathml.py", line 39, in doprint
unistr = mathML.toxml() AttributeError: 'str' object has no attribute 'toxml'我猜是图书馆出了点问题。
unistr = mathML.toxml()您可以查看此处的http://docs.sympy.org/dev/_modules/sympy/printing/mathml.html以查看该文件。
发布于 2015-01-28 07:34:38
正如@Emyen所指出的,问题在于你的输入是一个字符串。使用sympify将字符串转换为表达式,或者使用符号将表达式创建为Python表达式,例如
x = symbols('x')
expr = x**2 + 8*x + 16请参阅https://github.com/sympy/sympy/wiki/Idioms-and-Antipatterns#strings-as-input,了解为什么使用字符串而不是表达式是一个坏主意。
https://stackoverflow.com/questions/27659790
复制相似问题