I have string as follows:
s = \'key1=1234 key2=\"string with space\" key3=\"SrtingWithoutSpace\"\'
I want to convert in to a dictionary as
Try this:
>>> import re >>> dict(re.findall(r'(\S+)=(".*?"|\S+)', s)) {'key3': '"SrtingWithoutSpace"', 'key2': '"string with space"', 'key1': '1234'}
If you also want to strip the quotes:
>>> {k:v.strip('"') for k,v in re.findall(r'(\S+)=(".*?"|\S+)', s)}