On Mon, 2018-10-01 at 10:49 -0700, nanman3...@gmail.com wrote:
> I have a string like this: 
> 
> b'\tC:94.3%[S:89.9%,D:4.4%],F:1.7%,M:4.0%,n:1440\n'
> 
> And I would like to  extract the numbers corresponding to S,D,F and M in this 
> string and convert them into an array like this: 
> 
> [ '89.9', '4.4', '1.7', '4.0']
> 
> Any help would be appreciated! 
Hi there,

import re

s = b'\tC:94.3%[S:89.9%,D:4.4%],F:1.7%,M:4.0%,n:1440\n'

pattern = re.compile('S:([0-9.]+)%,D:([0-9.]+)%\],F:([0-9.]+)%,M:([0-9.]+)')

stuff = [x for x in re.findall(pattern, s)[0]]

You may consider just getting everthing out of your string and then munging it 
to what ever you like.

I mean:

pattern = re.compile('([A-Za-z]):([0-9.]+)')
{x: float(y) for x, y in re.findall(pattern, s)


Then you could read what ever and put it in a handy format wherein it is clear 
what it was. I mean:

stuff = []
for s in iostream:
    stuff.append({x: float(y) for x, y in re.findall(pattern, s)})


HTH
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to