Python Forum
Extract numbers from string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Extract numbers from string (/thread-9786.html)



Extract numbers from string - ian - Apr-28-2018

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']


RE: Extract numbers from string - snippsat - Apr-28-2018

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']