Retrieving values ​​from a string

I am trying to extract values ​​from a string, I was trying to get re.match , but had no luck. Line:

 '/opt/ad/bin$ ./ptzflip\r\nValue = 1800\r\nMin = 0\r\nMax = 3600\r\nStep = 1\r\n' 

I tried:

  map(int,re.search("Value\s*=\s*").group(1)) 

as well as:

 '/opt/ad/bin$ ./ptzflip\r\nValue = 1800\r\nMin = 0\r\nMax = 3600\r\nStep = 1\r\n'.split(' = ') 

I'm not sure what else to add or do. I want to get the attributes 'Value, Max, Step' and their values. Is there any way to do this?

Thanks for any help

+4
source share
4 answers

For this particular line, the following example parses it in a dictionary:

 s = '/opt/ad/bin$ ./ptzflip\r\nValue = 1800\r\nMin = 0\r\nMax = 3600\r\nStep = 1\r\n' d = {} for pair in [val.split('=') for val in s.split('\r\n')[1:-1]]: d[pair[0]] = int(pair[1]) 
+6
source
 >>> s = '/opt/ad/bin$ ./ptzflip\r\nValue = 1800\r\nMin = 0\r\nMax = 3600\r\nStep = 1\r\n' >>> bits = s.split('\r\n') >>> val, max_val, step = [int(bits[i].partition(' = ')[2]) for i in [1, 3, 4]] >>> val 1800 >>> max_val 3600 >>> step 1 
+3
source
 s = '/opt/ad/bin$ ./ptzflip\r\nValue = 1800\r\nMin = 0\r\nMax = 3600\r\nStep = 1\r\n' data = {} for l in s.split('\r\n'): if " = " in l: k,v = l.split(" = ") data[k] = int(v) print data 
0
source

You are trying to use regexp, but I think you can just split it into \r\n and then use the values ​​with = .

Sort of:

 s = '/opt/ad/bin$ ./ptzflip\r\nValue = 1800\r\nMin = 0\r\nMax = 3600\r\nStep = 1\r\n' dct = {} arr = [ss for ss in s.split('\r\n') if '=' in ss] for e in arr: k, v = e.split(' = ') dct[k] = v print dct 
0
source

All Articles