How to parse the string " {'result':(Boolean, MessageString)} "
using Python regular expressions to get Boolean
and the MessageString
separated into variables?
views:
112answers:
3
+1
A:
It looks like a dictionary+tuple in Python syntax, so eval() would also work (if you trust the source!!!)
Wim
2009-10-30 14:55:35
A:
Depending on what you need there can be other solution than regular expression. For this particular example the following code will work:
>>> compile("{'result':(Boolean, MessageString)}", '<expr>', 'eval').co_names
('Boolean', 'MessageString')
There is also parser
module which might be useful for such tasks.
Denis Otkidach
2009-10-30 15:03:16
+2
A:
This works:
>>> x = re.search('\((.*),\s*(.*)\)', " {'result':(Boolean, MessageString)} ")
>>> x.group(1)
'Boolean'
>>> x.group(2)
'MessageString'
Graeme Perrow
2009-10-30 15:06:55