使用eval()
执行表达式时,某些输入的输出错误。我认为不考虑十进制值。
我一直在尝试使用python eval()
评估字符串,还尝试了numexpr()
def _calc_line_base_amount(text):
num = '0123456789*-+/()'
# here text = "3/2*5"
if text:
for char in text:
if char not in num:
text = text.replace(char, "")
return eval(str(text))
并且也尝试过这个
import numexpr as ne
print(ne.evaluate("3/2*5"))
输入3/2 * 5预期产量7.5实际输出为5
编辑::在python3 +中有效。但我正在运行此python2.7
两者都工作正常并且提供相同的输出
使用函数eval()7.5
使用numexpr numexpr7.5
确保已为第二个安装了nuexpr,并确保您正在使用python3。