split a string of space separated substrings - elegant solution?

Wojciech Muła wojciech_mula at poczta.null.onet.pl.invalid
Tue Jul 31 16:57:46 EDT 2007


Helmut Jarausch wrote:
> Hi,
>
> 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')

import csv

s = 'abc "xy z"  "1 2 3"  "a \\" x"'
r = iter(csv.reader([s], delimiter=" ", escapechar="\\"))
print r.next()

w.



More information about the Python-list mailing list