I have a string of in Python
str1 = 'abc(1),bcd(xxx),ddd(dfk dsaf)'
How to use re to parse it into an object say 'results' so I can do something like:
for k,v in results:
print('key = %r, value = %r', (k, v))
Thanks.
Something like this, using re.findall:
>>> str1 = 'abc(1),bcd(xxx),ddd(dfk dsaf)'
>>> results = re.findall(r'(\w+)\(([^)]+)\),?',str1)
for k,v in results:
print('key = %r, value = %r' % (k, v))
...
key = 'abc', value = '1'
key = 'bcd', value = 'xxx'
key = 'ddd', value = 'dfk dsaf'
Pass it to dict() if you want a dict:
>>> dict(results)
{'bcd': 'xxx', 'abc': '1', 'ddd': 'dfk dsaf'}
key=abc value=1?