parsing parenthesized list in python's imaplib

前端 未结 3 2054
既然无缘
既然无缘 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条回答
  •  猫巷女王i
    2021-01-16 07:30

    pyparsing's nestedExpr parser function parses nested parentheses by default:

    from pyparsing import nestedExpr
    
    text = '(BODYSTRUCTURE ("text" "plain" ("charset" "ISO-8859-1") NIL NIL "quotedprintable" 1207 50 NIL NIL NIL NIL))'
    
    print nestedExpr().parseString(text)
    

    prints:

    [['BODYSTRUCTURE', ['"text"', '"plain"', ['"charset"', '"ISO-8859-1"'], 'NIL', 'NIL', '"quoted printable"', '1207', '50', 'NIL', 'NIL', 'NIL', 'NIL']]]
    

    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:

    from pyparsing import (nestedExpr, Literal, Word, alphanums, 
        quotedString, replaceWith, nums, removeQuotes)
    
    NIL = Literal("NIL").setParseAction(replaceWith(None))
    integer = Word(nums).setParseAction(lambda t:int(t[0]))
    quotedString.setParseAction(removeQuotes)
    content = (NIL | integer | Word(alphanums))
    
    print nestedExpr(content=content, ignoreExpr=quotedString).parseString(text)
    

    Prints:

    [['BODYSTRUCTURE', ['text', 'plain', ['charset', 'ISO-8859-1'], None, None, 'quoted-printable', 1207, 50, None, None, None, None]]]
    

提交回复
热议问题