Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Values from a String

Tags:

python

string

I am trying to extract values from a string, I have tried to get re.match working but have not had any luck. The string is:

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

I have tried:

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

and also:

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

I am not sure what else to add or do. I want to retrieve the attributes 'Value, Max, Step' and their values. Is there anyway to do this?

Thanks for any help

like image 715
chrisg Avatar asked Jan 22 '23 17:01

chrisg


1 Answers

For that particular string, the following parses it into 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])
like image 184
ezod Avatar answered Feb 02 '23 09:02

ezod