I would like to convert a String like:
s = "[2-1,2,3]"
into a list, like:
a = [1,2,3]
I tried it with json:
s = "[2-1,2,3]"
a = json.loads(s)
but it can't handle 2-1.
Is there an easy method to convert strings into any kind of datatype?
I would like to convert a String like:
s = "[2-1,2,3]"
into a list, like:
a = [1,2,3]
I tried it with json:
s = "[2-1,2,3]"
a = json.loads(s)
but it can't handle 2-1.
Is there an easy method to convert strings into any kind of datatype?
 
    
     
    
    Yes. And as much as it pains me to say it, eval is your friend here, as even ast.literal_eval cannot parse this.
Example code:
import re
s = "[2-1,2,3]"
rexp = re.compile('[\d-]+')
out = []
for exp in rexp.findall(s):
    out.append(eval(exp))
Or, if you prefer a one-liner:
out = [eval(exp) for exp in rexp.findall(s)]
Output:
[1, 2, 3]
 
    
    This is a common problem to tackle while writing compilers. Usually this comes under lexing. A parser would usually have a list of tokens, watch for the tokens and then pass it to a parser and then the compiler.
Your problem cannot be completely solved with a lexer though since you also require the 2-1 to evaluate to 1. In this case, I would suggest using eval like @Daniel Hao suggested since it is a simple and clean way of achieving your goal. Remember about the caveats(both security and otherwise) while using it though. (especially, in production)
If you are interested in the parsing though, check this out:
