将字符串转换为SymPy表达式?

时间:2017-03-22 01:59:10

标签: python python-3.x sympy

我试图将字符串转换为SymPy表达式。我已经尝试了Option Explicit Sub InsertShapeRxC() Dim strInput As String Dim lngRows As Long, lngColumns As Long Dim rngShape As Range Dim ws As Worksheet Dim shp As Shape ' get user input as string strInput = Application.InputBox("Please enter RxC", Type:=2) ' get rows and columns from input - expected RxC lngRows = Split(strInput, "x", -1, vbTextCompare)(0) lngColumns = Split(strInput, "x", -1, vbTextCompare)(1) ' resize current selection to rows and columns as input Set rngShape = Selection Set rngShape = rngShape.Resize(lngRows, lngColumns) ' get reference to worksheet Set ws = rngShape.Parent ' add shape Set shp = ws.Shapes.AddShape(1, rngShape.Left, rngShape.Top, rngShape.Width, rngShape.Height) With shp .Fill.Visible = msoFalse With .Line .Visible = msoTrue .ForeColor.RGB = RGB(0, 0, 0) .Transparency = 0 End With End With With rngShape .Borders(xlInsideHorizontal).LineStyle = xlContinuous .Borders(xlInsideVertical).LineStyle = xlContinuous End With End Sub Sub InsertShapeWithRange() Dim strInput As String Dim lngRows As Long, lngColumns As Long Dim rngShape As Range Dim ws As Worksheet Dim shp As Shape ' get user input as string Set rngShape = Application.InputBox("Please enter range", Type:=8) ' get reference to worksheet Set ws = rngShape.Parent ' add shape Set shp = ws.Shapes.AddShape(1, rngShape.Left, rngShape.Top, rngShape.Width, rngShape.Height) With shp .Fill.Visible = msoFalse With .Line .Visible = msoTrue .ForeColor.RGB = RGB(0, 0, 0) .Transparency = 0 End With End With With rngShape .Borders(xlInsideHorizontal).LineStyle = xlContinuous .Borders(xlInsideVertical).LineStyle = xlContinuous End With End Sub sympify(),但两者都返回了错误。如何使用替代方法更正这些错误或解决这些错误?我使用的是Python 3.4。

parse_expr()

这两个函数都会返回以下错误:

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)

1 个答案:

答案 0 :(得分:1)

正如同情的documentation解释:

  

[T]他O被解释为Order对象(与系列一起使用),如果使用不当会引发错误[。]

它提出了一些解决方法,我更喜欢的是使用碰撞本地:

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