Splitting a quoted string.

Gerard Flanagan grflanagan at yahoo.co.uk
Wed May 16 09:13:37 EDT 2007


On May 16, 12:42 pm, mosscliffe <mcl.off... at googlemail.com> wrote:
> I am looking for a simple split function to create a list of entries
> from a string which contains quoted elements.  Like in 'google'
> search.
>
> eg  string = 'bob john "johnny cash" 234 june'
>
> and I want to have a list of ['bob', 'john, 'johnny cash', '234',
> 'june']
>
> I wondered about using the csv routines, but I thought I would ask the
> experts first.
>
> There maybe a simple function, but as yet I have not found it.
>

See 'split' from 'shlex' module:

>>> s = 'bob john "johnny cash" 234 june'
>>> import shlex
>>> shlex.split(s)
['bob', 'john', 'johnny cash', '234', 'june']
>>>





More information about the Python-list mailing list