Python 将字符串转换为可计算操作
是否可以将字符串转换为可计算的操作 我想做到这一点:Python 将字符串转换为可计算操作,python,string,math,Python,String,Math,是否可以将字符串转换为可计算的操作 我想做到这一点: >>> import math >>> operation = "10/2*6 + math.sqrt(42)" >>> compute(operation) 36.48074069840786 eval将为您执行此操作 >>> import math >>> operation = "10/2*6 + math.sqrt(42)" >>&
>>> import math
>>> operation = "10/2*6 + math.sqrt(42)"
>>> compute(operation)
36.48074069840786
eval
将为您执行此操作
>>> import math
>>> operation = "10/2*6 + math.sqrt(42)"
>>> eval(operation)
36.48074069840786
当然,这就是内置的
eval
所做的。。。但是这样做可能会导致安全问题。不是吗?literaleval
可能会重复,它会更安全一些。@InbarRoseliteral\u eval()
不适用于数学表达式。eval(operation.replace(“,”),{uuuuuuuuuuuuuu内置项]:None,'math':math}可能会更安全一些。。。