split a string of space separated substrings - elegant solution?

Paul McGuire ptmcg at austin.rr.com
Wed Aug 1 00:16:43 EDT 2007


On Jul 31, 3:30 pm, Helmut Jarausch <jarau... at skynet.be> wrote:
> I'm looking for an elegant solution to the following (quite common)
> problem:
>
> Given a string of substrings separated by white space,
> split this into tuple/list of elements.
> The problem are quoted substrings like
>
> abc "xy z"  "1 2 3"  "a \" x"
>
> should be split into  ('abc','xy z','1 2 3','a " x')
>

Pyparsing has built-in support for special treatment of quoted
strings.  Observe:

from pyparsing import *

data = r'abc "xy z"  "1 2 3"  "a \" x"'

quotedString.setParseAction(removeQuotes)
print OneOrMore(quotedString |
                    Word(printables) ).parseString(data)

prints:

['abc', 'xy z', '1 2 3', 'a \\" x']

Or perhaps a bit trickier, do the same while skipping items inside /*
*/ comments:

data = r'abc /* 456 "xy z" */  "1 2 3"  "a \" x"'

quotedString.setParseAction(removeQuotes)
print OneOrMore(quotedString |
                    Word(printables) ) \
                    .ignore(cStyleComment).parseString(data)

prints:

['abc', '1 2 3', 'a \\" x']


-- Paul




More information about the Python-list mailing list