Next / Previous / Contents / Shipman's homepage

6.3. delimitedList(): Create a parser for a delimited list

delimitedList(parser, delim=",", combine=False)

This function creates a parser for a sequence P D P D ... D P, where P matches some parser and D is some delimiter, defaulting to “,”.

By default, the result is a list of the P items with the delimiters (D items) omitted.

>>> text = "Arthur, Bedevere, Launcelot, Galahad, Robin"
>>> name = pp.Word(pp.alphas)
>>> nameList = pp.delimitedList(name)
>>> print nameList.parseString(text)
['Arthur', 'Bedevere', 'Launcelot', 'Galahad', 'Robin']

To include the delimiters and fuse the entire result into a single string, pass in the argument combine=True.

>>> allNames = pp.delimitedList(name, delim=', ', combine=True)
>>> print allNames.parseString(text)
['Arthur, Bedevere, Launcelot, Galahad, Robin']
>>> badExample = pp.delimitedList(name, combine=True)
>>> print badExample.parseString(text)
['Arthur']

The last example only matches one name because the Combine class suppresses the skipping of whitespace within its internal pieces.