split a string of space separated substrings - elegant solution?

Carsten Haese carsten at uniqsys.com
Tue Jul 31 17:07:52 EDT 2007


On Tue, 2007-07-31 at 22:30 +0200, 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 shlex
>>> shlex.split('abc "xy z"  "1 2 3"  "a \\" x"')
['abc', 'xy z', '1 2 3', 'a " x']

I hope that's elegant enough ;)

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list