String formation to independent variables

Question:

String = 'var1 = hello, var2 = bye'

So i have this string that is being returned from a webpage and im trying to turn this into this:

   var1 = 'Hello'
   var2 = 'bye'

I was wondering if this could be done using .format or something else.

one of my biggest problem is time, i want the fasted method.

Asked By: kevinjonson133

||

Answers:

Use a dictionary to store key value pair

data = 'var1 = hello, var2 = bye' # the data returned from a web page

dmap = {}

for value in data.split(","):
    array = value.split("=")
    dmap[array[0].strip()] = array[1].strip()

print(dmap)
Answered By: devp
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.