Re: split a string of space separated substrings - elegant solution?



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:

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
.



Relevant Pages

  • Re: CSV Reader
    ... I've used your advice Andrew and tried to use the CSV module, ... Is this because of the way the CSV module is reading the data in? ... for row in reader: ...
    (comp.lang.python)
  • Re: parsing a dbIII file
    ... The CSV module is probably the easiest way to go: ... for row in reader: ... '2', '3', 'other string'] ...
    (comp.lang.python)