smart splitting - how to
jarausch at igpm.rwth-aachen.de
Fri Dec 13 12:55:37 CET 2013
On Fri, 13 Dec 2013 11:39:57 +0000, Chris Angelico and Robert Kern wrote:
> On 2013-12-13 11:28, Helmut Jarausch wrote:
>> I'd like to read several strings by using 'input'.
>> These strings are separated by white space but I'd like to allow for
>> some quoting, e.g.
>> "Guido van" Rossum
>> should be split into 2 strings only
>> Now, a simple split doesn't work since it splits the quoted text as well.
>> Is there a simple way to do so?
>> It would be nice if it could handle embedded quotes which are escaped
>> by a backslash, too.
>> Is there something simpler then a sophisticated regular expression
>> or even a parser?
> |1> import shlex
> |2> shlex.split(r'"Guido \"van\" Rossum" invented Python')
> ['Guido "van" Rossum', 'invented', 'Python']
Many thanks, that works perfectly and is so simple.
More information about the Python-list