Taking Brian's pyparsing decision in another step, you can create a quasi-deserializer for this format using the Dict class:
import pyparsing string = pyparsing.CharsNotIn("{} \t\r\n") # use Word instead of CharsNotIn, to do whitespace skipping stringchars = pyparsing.printables.replace("{","").replace("}","") string = pyparsing.Word( stringchars ) # define a simple integer, plus auto-converting parse action integer = pyparsing.Word("0123456789").setParseAction(lambda t : int(t[0])) group = pyparsing.Forward() group << ( pyparsing.Group(pyparsing.Literal("{").suppress() + pyparsing.ZeroOrMore(group) + pyparsing.Literal("}").suppress()) | integer | string ) toplevel = pyparsing.OneOrMore(group) sample = """ protocol sample_thread { { AUTOSTART 0 } { BITMAP thread.gif } { COORDS {0 0} } { DATAFORMAT { { TYPE hl7 } { PREPROCS { { ARGS {{}} } { PROCS sample_proc } } } } } } """ print toplevel.parseString(sample).asList() # Now define something a little more meaningful for a protocol structure, # and use Dict to auto-assign results names LBRACE,RBRACE = map(pyparsing.Suppress,"{}") protocol = ( pyparsing.Keyword("protocol") + string("name") + LBRACE + pyparsing.Dict(pyparsing.OneOrMore( pyparsing.Group(LBRACE + string + group + RBRACE) ) )("parameters") + RBRACE ) results = protocol.parseString(sample) print results.name print results.parameters.BITMAP print results.parameters.keys() print results.dump()
Print
['protocol', 'sample_thread', [['AUTOSTART', 0], ['BITMAP', 'thread.gif'], ['COORDS', [0, 0]], ['DATAFORMAT', [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]]]]] sample_thread thread.gif ['DATAFORMAT', 'COORDS', 'AUTOSTART', 'BITMAP'] ['protocol', 'sample_thread', [['AUTOSTART', 0], ['BITMAP', 'thread.gif'], ['COORDS', [0, 0]], ['DATAFORMAT', [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]]]]] - name: sample_thread - parameters: [['AUTOSTART', 0], ['BITMAP', 'thread.gif'], ['COORDS', [0, 0]], ['DATAFORMAT', [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]]]] - AUTOSTART: 0 - BITMAP: thread.gif - COORDS: [0, 0] - DATAFORMAT: [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]]
I think you will grow faster with pyparsing.
- Paul