Splitting with quoted strings?
Robert Roy
rjroy at takingcontrol.com
Mon May 7 21:20:16 EDT 2001
On Mon, 07 May 2001 15:03:53 -0700, Chris Barker
<chrishbarker at home.net> wrote:
>Is there any nifty way to do a string.split() that doesn't split inside
>quotes?
>
>what I want is (for example):
>
>'this that "the other" some more'.split()
>
>to give me ['this', 'that', 'the other', 'some', 'more']
>
>I would ideally like it to be pretty fast. It turns out this is pretty
>common in a bunch of the text files I need to read, and it is also a
>form put out by Excel, and used for *nix command lines, etc., so I
>figured someone might have written a nice extension to do this.
>
>
>Thanks, Chris
>
The ASV library at:
http://tratt.net/~laurie/python/asv/
is probably what you need.
Based on your example, I created a subclass SSV (Space Separated
Value)
>>> s = 'this that "the other thing" some more'
>>> class SSV(ASV.SimpleSV):
... _SEPERATORS = [" "]
... _WHITESPACE = ["\t"]
...
>>> a.input(s, SSV())
>>> a
[['this', 'that', 'the other thing', 'some', 'more']]
Bob
More information about the Python-list
mailing list