Home

On Jul 31, 3:30 pm, Helmut Jarausch <jarau...@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

previous
next

Re: why should I learn python
Re: Inserting an element into existing xml file
Re: PyS60
Re: help with vector<vector<double>>
decorators - more than just syntactic sugar
Nasze Dzieci
Pajacyk
Krwinka
Mam Marzenie
Fundacja Sloneczko