Python Forum

Full Version: Extract numbers from string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to find way to extract numbers from a string but the following not quick right. I need to get [98.15, 0.97, 1.00, 118506]

import re
a = 'C.1 str  98.15  +0.97increase  +1.00%increase  118,506  04/27/18'
re.findall(r"([\d.]*\d+)", a)
['.1', '98.15', '0.97', '1.00', '118', '506', '04', '27', '18']
something like this.
>>> import re
>>> 
>>> s = 'C.1 str  98.15  +0.97increase  +1.00%increase  118,506  04/27/18'
>>> lst = re.findall(r'\d+\.\d+|\d+\,\d+', s)
>>> lst
['98.15', '0.97', '1.00', '118,506']
>>> [c.replace(',', '') for c in lst]
['98.15', '0.97', '1.00', '118506']