pyparsing: crash on empty element

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Mar 6 16:28:40 EST 2006


To indicate that a particular parse expression may be empty, pyparsing
includes the Optional element.

Change:
pp.delimitedList(withquotes | withoutquotes) + \

to:
pp.Optional( pp.delimitedList(withquotes | withoutquotes) ) + \

and I think this will have the desired effect.

-- Paul


<gry at ll.mit.edu> wrote in message
news:1141678685.123733.275150 at u72g2000cwu.googlegroups.com...
> [python 2.3.3, pyparsing 1.3]
> I have:
>
>  def unpack_sql_array(s):
>     # unpack a postgres "array", e.g. "{'w1','w2','w3'}" into a
> list(str)
>     import pyparsing as pp
>     withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes)
>     withoutquotes = pp.CharsNotIn('",{}')
>     parser = pp.StringStart() + \
>              pp.Literal('{').suppress() + \
>              pp.delimitedList(withquotes | withoutquotes) + \
>              pp.Literal('}').suppress() + \
>              pp.StringEnd()
>     return parser.parseString(s).asList()
>
> which works beautifully, except on the input: "{}".  How can I neatly
> modify the parser to return an empty list in this case?
> Yes, obviously, I could say
>    if s=='{}': return []
> It just seems like I'm missing some simple intrinsic way to get this
> out of the parser.  I am hoping to become more skillful in using the
> wonderful pyparsing module!
>
> -- George Young
>





More information about the Python-list mailing list