Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
split a string of space separated substrings - elegant solution?
- From: ptmcg at austin.rr.com (Paul McGuire)
- Subject: split a string of space separated substrings - elegant solution?
- Date: Tue, 31 Jul 2007 21:16:43 -0700
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
- Follow-Ups:
- split a string of space separated substrings - elegant solution?
- From: Helmut Jarausch
- split a string of space separated substrings - elegant solution?
- Prev by Date: Python end of file marker similar to perl's __END__
- Next by Date: Any way to monitor windows network connection?
- Previous by thread: Python end of file marker similar to perl's __END__
- Next by thread: split a string of space separated substrings - elegant solution?
- Index(es):