Let's say I have a string like this:
"(((((908488990) - (255272197)) + ((-287634246) - (1144917742))) + (((1178779462) + (1410551276)) - ((-244815224) - (-722994984)))) + ((((-1134622847) - (-818189911)) + ((-479615696) + (-938948012))) + (((-1762293529) + (1281608170)) - ((1468184728) + (-895014314)))))"
How do I convert it to become an expression like this (without quotes) in Python:
(((((908488990) - (255272197)) + ((-287634246) - (1144917742))) + (((1178779462) + (1410551276)) - ((-244815224) - (-722994984)))) + ((((-1134622847) - (-818189911)) + ((-479615696) + (-938948012))) + (((-1762293529) + (1281608170)) - ((1468184728) + (-895014314)))))
So that I can get the result of that expression:
-1457036634
use eval
eval("(((((908488990) - (255272197)) + ((-287634246) - (1144917742))) + (((1178779462) + (1410551276)) - ((-244815224) - (-722994984)))) + ((((-1134622847) - (-818189911)) + ((-479615696) + (-938948012))) + (((-1762293529) + (1281608170)) - ((1468184728) + (-895014314)))))")
Using eval might be hazardous in case you want to process string from user inputs as mentionned by @Hymns-For-Disco.
Here is an example on how to process such string, it's a bit long and parts can be placed in sub functions but it gives the right output (plus parenthesis check):
test_string = r"(((((908488990) - (255272197)) + ((-287634246) - (1144917742))) + (((1178779462) + (1410551276)) - ((-244815224) - (-722994984)))) + ((((-1134622847) - (-818189911)) + ((-479615696) + (-938948012))) + (((-1762293529) + (1281608170)) - ((1468184728) + (-895014314)))))"
import operator
operators = {'+' : operator.add, '-' : operator.sub} # you can add other operators here like operator.multiply
def compute(string):
operator = None
value_left = None
it = iter(string)
while True:
try:
character = next(it)
except StopIteration:
break
if character ==' ': continue
if character == '(':
count = 1 #count open parenthesis
sub_string = ''
while True:
try:
character = next(it)
except StopIteration:
raise(EOFError("Not matching parenthesis"))
if character == '(':
count+=1
if character == ')':
if count == 1:
break
else:
sub_string+= character
count-=1
else:
sub_string+= character
if operator is None:
print('call compute with{}'.format(sub_string))
value_left = compute(sub_string)
continue
else:
return operator(value_left,compute(sub_string))
if character.isdigit():
temp_num = character
while character.isdigit() or character=='.':
try:
character = next(it)
except StopIteration:
break
temp_num+= character
if operator is None:
value_left = float(temp_num)
else:
return operator(value_left, float(temp_num))
if character in operators.keys():
operator = operators[character]
#test for unary '-' operator:
if character == '-' and value_left is None:
value_left = 0.0
return value_left
print(compute(test_string)) #returns -1457036634.0
test_string =r'(3+4'
print(compute(test_string)) # raises EOFError: Not matching parenthesis
Hope it will help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.