2017-03-22 49 views
0

我想將字符串轉換爲SymPy表達式。我試過sympify()parse_expr(),但都返回錯誤。我如何糾正這些錯誤或使用替代方法解決它們?我正在使用Python 3.4。將字符串轉換爲SymPy表達式?

from sympy import * 
s = 'C+O*2' 
expr = sympify(s) 

from sympy.parsing.sympy_parser import parse_expr 
s = 'C+O*2' 
expr = parse_expr(s) 

這兩個函數返回以下錯誤:

File "C:\...\sympy\core\mul.py", line 182, in flatten 
    r, b = b.as_coeff_Mul() 

TypeError: as_coeff_Mul() missing 1 required positional argument: 'self' 

回答

1

由於documentation爲sympify解釋說:

[T]he O is interpreted as the Order object (used with series) and it raises an error when used improperly[.]

它提出了一些解決方法,和一個我寧願是用衝突當地人:

>>> from sympy.abc import _clash1 
>>> sympify("C+O*2", locals=_clash1) 
C + 2*O 
+0

完美。謝謝! – zdub