Re: split a string of space separated substrings - elegant solution?
- From: "Jerry Hill" <malaclypse2@xxxxxxxxx>
- Date: Tue, 31 Jul 2007 17:11:04 -0400
On 7/31/07, Helmut Jarausch <jarausch@xxxxxxxxx> 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')
Using the csv module gets you most of the way there. For instance:
print rowimport csv
text = r'abc "xy z" "1 2 3" "a \" x"'
reader = csv.reader([text], delimiter=" ", escapechar='\\')
for row in reader:
['abc', 'xy z', '', '1 2 3', '', 'a " x']
That does leave you with empty elements where you had double spaces
between items though. you could fix that with something like:
row = [element for element in row if element != '']for row in reader:
print row
['abc', 'xy z', '1 2 3', 'a " x']
The CSV module can handle lots of delimited data other that quote and
comma delimited. See the docs at:
http://docs.python.org/lib/module-csv.html and PEP 305:
http://www.python.org/dev/peps/pep-0305/
--
Jerry
.
- References:
- split a string of space separated substrings - elegant solution?
- From: Helmut Jarausch
- split a string of space separated substrings - elegant solution?
- Prev by Date: Re: split a string of space separated substrings - elegant solution?
- Previous by thread: Re: split a string of space separated substrings - elegant solution?
- Next by thread: Live editing...
- Index(es):
Relevant Pages
|