split a string of space separated substrings - elegant solution?

Jerry Hill malaclypse2 at gmail.com
Tue Jul 31 17:11:04 EDT 2007


On 7/31/07, Helmut Jarausch <jarausch 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')

Using the csv module gets you most of the way there.  For instance:

>>> import csv
>>> text = r'abc "xy z"  "1 2 3"  "a \" x"'
>>> reader = csv.reader([text], delimiter=" ", escapechar='\\')
>>> for row in reader:
	print row
	
['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:

>>> for row in reader:
	row = [element for element in row if element != '']
	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



More information about the Python-list mailing list