I have a string in this format "[12.45,56.78]" , How to extract the float numbers and assign them in two different variables . newbie in python please help
            Asked
            
        
        
            Active
            
        
            Viewed 58 times
        
    -3
            
            
        - 
                    http://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python – Padraic Cunningham Aug 19 '15 at 18:38
- 
                    While we seem to be playing code golf... `x,y = map(float, re.findall('\d+\.\d+', s))` – Cory Kramer Aug 19 '15 at 18:39
- 
                    `a,b = map(float, s.strip("[]").split(","))` – Padraic Cunningham Aug 19 '15 at 18:41
- 
                    d = [float(e) for e in ("[12.45,56.78]")[1:-1].split(",")] – dsgdfg Aug 19 '15 at 18:43
2 Answers
2
            
            
        or
x = "[12.45,56.78]"
myList= eval(x)
myList[0]
Out[1]: 12.45
myList[1]
Out[2]: 56.78
 
    
    
        Timo Kvamme
        
- 2,806
- 1
- 19
- 24
1
            You can use ast.literal_eval() to evaluate the string and convert to list and then unpack them into two variables. Example -
>>> import ast
>>> a,b = ast.literal_eval("[12.45,56.78]")
>>> a
12.45
>>> b
56.78
 
    
    
        Anand S Kumar
        
- 88,551
- 18
- 188
- 176
