Parsing parameters with quotes

John Machin sjmachin at lexicon.net
Sun Mar 16 03:33:04 CET 2003


bokr at oz.net (Bengt Richter) wrote in message news:<b50acl$b70$0 at 216.39.172.122>...
> On 14 Mar 2003 15:02:32 -0800, sjmachin at lexicon.net (John Machin) wrote:
> >"Giovanni Bajo" <noway at sorry.com> wrote in message news:<Ywaca.11032$Lr4.323544 at twister2.libero.it>...
> >> My input is:
> >> 'foo "this is one" and this not':
> >> and I want to output:
> >> ["foo", "this is one", "and", "this", "not"]
> >> 
> >> Basically, a string.split() but must take into account quotes used to group
> >> as a single word (no escaping is supported within quotes). Now, is there
> >> already something in the python library to do this? My code is a bit longer
> >> than I would have expected:
> >> Is there any faster way? getopt() does not seem to do this (it's done
> >> beforehand by whoever fills sys.argv[])
> >
> >See the following code. Version 2 reproduces the results of (your)
> >Version 1. Should be faster, but I haven't tested this as I was more
> >concerned with correctness. I would expect that any argument
> >surrounded by "" should survive with its contents unmangled. In the
> >worst case an empty arg "" doesn't even survive. Version 3 below gives
> >the serendipitous outcome of simpler code plus less disturbing (to me
> >anyway) results.
> >
> >Hope this helps,
> >John
> >
> I rearranged the test loops to test by function outermost, and reformattted
> the output a little, and added SplitParms4. The result follows, replacing
> what was within your snip lines:
[snip]

Bengt, Thanks for the cosmetics :-)
I take it you didn't disagree with my extrapolation of reasonable user
requirements from the OP's one example.

> 
> import re
> def SplitParms4(s, p=re.compile(r'"(.*?)"|(\S+)')):
>     return [g2 or g1 for g1,g2 in p.findall(s)]

Always striving for the last word :

def SplitParms4a(s, pf=re.compile(r'"(.*?)"|(\S+)').findall):
     return [g2 or g1 for g1,g2 in pf(s)]

Cheers,
John




More information about the Python-list mailing list