All i need to do is convert a String into an simple object like:
"1/4*x+ 1" to 1/4*x+ 1
I am extracting text from image and calculating the expression in the image.
If i get:
valid_text = 1/4*x+ 1= 1/6*x+ 1/2
from image, I am doing:
if 'x' in valid_text and '=' in valid_text:
lhs = valid_text.rsplit('=', 1)[0]
lhs = eval(lhs)
rhs = valid_text.rsplit('=', 1)[1]
rhs = eval(rhs)
solution = solve(Eq(lhs, rhs), x)
I am using eval()
function to return an object when i pass a String into it. But eval()
function sometimes gives
rhs = eval(rhs)
File "<string>", line 1, in <module>
TypeError: 'int' object is not callable` **AND other inappropriate outputs.**
Is there any other way to get an object from String?
This worked for me: