parsing parenthesized list in python's imaplib

前端 未结 3 2016
既然无缘
既然无缘 2021-01-16 07:15

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         


        
3条回答
  •  太阳男子
    2021-01-16 07:44

    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(' ')]))

提交回复
热议问题