Splitting a line while keeping quoted items together

Chris Rebert clp2 at rebertia.com
Tue Nov 20 01:21:09 CET 2012


On Monday, November 19, 2012, wrote:

> I am working on a cmd.Cmd-based program, and normally could just split the
> string and get the right parts.
>
> Now I have a case where I could have two or three words in the string that
> need to be grouped into the same thing.
>
> Then I realized that I'm not the only person who has had to deal with
> this, and I'm wondering if my solution is the best one out there or if this
> is as ugly at it feels?
>
> Code below
> .......
>
> #x('Seattle 456') -> ('Seattle', '456')
> #x('"Portland Alpha" 123') -> ('Portland Alpha', '123')
> #x("'Portland Beta' 789') -> ('Portland Beta', '789')
>
>  <snipped code defining function x()>

This seem really ugly. Is there a cleaner way to do this? Is there a
> keyword I could search by to find something nicer?
>

Use the "shlex" module in the std lib?

Cheers,
Chris


-- 
Cheers,
Chris
--
http://rebertia.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121119/beaf7cf5/attachment.html>


More information about the Python-list mailing list