I am looking for simple way to split parenthesized lists that come out of IMAP responses into Python lists or tuples. I want to go from
\'(BODYSTRUCTURE (\"t
The fact that there's nested tuples makes this impossible with a regex. You'll have to write a parser to denote when you're inside a parenthesis or not.
You could try
tuple('(BODYSTRUCTURE ("text" "plain" ("charset" "ISO-8859-1") NIL NIL "quoted-printable" 1207 50 NIL NIL NIL NIL))'.replace("NIL", "None").split(' '))
Edit: Well I got something that works with your example, not sure it's what you want though.
BODYSTRUCTURE needs to be defined somewhere.
eval(",".join([a for a in '(BODYSTRUCTURE ("text" "plain" ("charset" "ISO-8859-1") NIL NIL "quoted-printable" 1207 50 NIL NIL NIL NIL))'.replace("NIL", "None").split(' ')]))