I want to get numeric data from string. How to do that in python? -
i want numeric data string. how in python???
for e.g. string "data23/45 data" extract 23 , 45
i'm sure there hundreds of ways, 1 way use regular expression split string digit groups, , list comprehension convert values integers;
>>> import re >>> = "data23/45 data" >>> [int(x) x in re.split('[^\d]+', a) if x] [23, 45]
re.split('[^\d]+', a)
split on non digits, leaves digit groups in result. outer list comprehension convert non empty values in result integers.
of course, if want digit groups still strings, can use simpler;
>>> [x x in re.split('[^\d]+', a) if x] ['23', '45']
Comments
Post a Comment