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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -