SymPy的Sympify函数返回一个string,而不是一个SymPy对象

我有一个两列的Excel文件; 第一个包含我的variables名称,第二个包含一个键入mathexpression式的string。 使用Python 2.7.12和openpyxl 2.3.2,我正在导入这些数据。 该string默认导入为unicode。

我想从我导入的string创build一个Sympy对象来执行一些操作。 但是,在这种情况下sympifying我的导入string返回一个string对象,而不是一个SymPy对象。 为什么是这样?

我得到预期的行为(sympify返回一个SymPy对象),如果我反而硬编码string到我的.py文件。

此代码说明:

from sympy import * import openpyxl imported_data = openpyxl.load_workbook('sample_workbook.xlsx') data = imported_data.get_sheet_by_name('sheet1') variable_name = data['A1'].value variable_value = data['B1'].value globals()[variable_name] = variable_value x = symbols('x') print(type(variable1)) # This is a unicode string sympy_expr = sympify(variable1) print(type(sympy_expr)) # This should now be a SymPy object, but it's now of type str 

工作簿截图

工作簿下载