For example :
je = ("3,5&6,7")
And the result that I want is [3,5,6,7]
What I have tried so far:
je = ("3,5&6,7")
jean = je.split(",")
j = str(jean)
De = j.split("&")
print De
But my result is ["['3', '5", "6', '7']"]
Use regex
:
In [3]: import re
In [4]: strs = "3,5&6,7"
#map
In [5]: map(int, re.findall(r'\d+',strs))
Out[5]: [3, 5, 6, 7]
#LC
In [6]: [int(x) for x in re.findall(r'\d+',strs)]
Out[6]: [3, 5, 6, 7]
You can use replace()
along with ast.literal_eval
:
>>> import ast
>>> je = ("3,5&6,7")
>>> ast.literal_eval(je.replace('&', ','))
(3, 5, 6, 7)
Or if you really need a list:
>>> ast.literal_eval('['+je.replace('&', ',')+']')
[3, 5, 6, 7]