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 ("text" "plain" ("charset" "ISO-8859-1") NIL NIL "quoted-printable" 1207 50 NIL NIL NIL NIL))'
to
(BODYSTRUCTURE, ("text", "plain", ("charset", "ISO-8859-1"), None, None, "quoted-printable", 1207, 50, None, None, None, None))
pyparsing’s nestedExpr parser function parses nested parentheses by default:
prints:
Here is a slightly modified parser, which does parse-time conversion of integer strings to integers, from “NIL” to None, and stripping quotes from quoted strings:
Prints: