Python custom string to dict

Advertisements

is there any smart way to convert python string in a format:
"name=text;name2=text2;name3=text3"
to python dict?

Maybe something with json.loads, but I’m not sure…

ANSWER:
so the smartest solution found so far is:

result = dict((a.strip(), b.strip())  
     for a, b in (element.split('=')  
           for element in oryginal_text.split(';')))  ```

>Solution :

string = "name=text;name2=text2;name3=text3"

args = dict(i.split('=') for i in string.split(';'))
print(args) # {'name': 'text', 'name2': 'text2', 'name3': 'text3'}

Leave a ReplyCancel reply