Re: smart splitting - how to
On Fri, Dec 13, 2013 at 10:28 PM, Helmut Jarausch jarau...@igpm.rwth-aachen.de wrote: 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. Sounds like you want shell-style splitting. Check out the shlex module: http://docs.python.org/3.3/library/shlex.html ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: smart splitting - how to
On 2013-12-13 11:28, Helmut Jarausch wrote: Hi, 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? http://docs.python.org/3.3/library/shlex [~] |1 import shlex [~] |2 shlex.split(r'Guido \van\ Rossum invented Python') ['Guido van Rossum', 'invented', 'Python'] -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- https://mail.python.org/mailman/listinfo/python-list
Re: smart splitting - how to
On Fri, Dec 13, 2013 at 6:28 AM, Helmut Jarausch jarau...@igpm.rwth-aachen.de wrote: Hi, 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? Many thanks for a hint, Helmut -- https://mail.python.org/mailman/listinfo/python-list Take a look at the csv reader module. You can set it to use space as field separator and also to handle quotes as field delimiters. This would leave your quoted strings with spaces as a single field. http://docs.python.org/2/library/csv.html#dialects-and-formatting-parameters I haven't tried your example, but I'm pretty sure it can handle it. -- Joel Goldstick http://joelgoldstick.com -- https://mail.python.org/mailman/listinfo/python-list
Re: smart splitting - how to
On Fri, 13 Dec 2013 11:39:57 +, Chris Angelico and Robert Kern wrote: On 2013-12-13 11:28, Helmut Jarausch wrote: Hi, 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? http://docs.python.org/3.3/library/shlex [~] |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. Helmut -- https://mail.python.org/mailman/listinfo/python-list