0

I was parsing a XML file using python, I got a variable assigned with below values

server = `sys:var1=value1,var2=value2,var3=value3`

can some one please assist me how can I parse this variable 'server' now. I want to fetch the individual values like

var1 = value1
var2 = value2
var3 = value3
1

1 Answer 1

1

You can use Python's regular expression to parse any string. for your requirement bellow code will give you list of variables(valiables) and corresponding value(values) in a list.

import re

server = 'sys:var1=value1,var2=value2,var3=value3'
variables = re.findall(r'([\w]+)=', server)
values = re.findall(r'=([\w]+)', server)

Result

variables: ['var1', 'var2', 'var3']
values: ['value1', 'value2', 'value3']

Brief Explanation
in the above code
([\w]+)=: finds one or more alpha-numeric and underscore( _ ) characters followed by =.
[\w]+: means select one or more alpha-numeric and underscore( _ ) characters
(...): groups those selected character and leaves behind = in result
You can refer bove link for more details about regex expression.

Sign up to request clarification or add additional context in comments.

1 Comment

You can accept the answer by clicking tick mark. And you can read more about regex for better manipulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.