Parsing parameters with quotes

Giovanni Bajo noway at sorry.com
Thu Mar 13 20:47:36 EST 2003


Hello,

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:

def SplitParms(s):
    s = s.split('"')

    L = []
    for i,t in zip(range(0,len(s)), s):
        if t:
            if i%2 == 1:
                L.append(t.strip())
            else:
                L.extend(t.split())

    return L

Is there any faster way? getopt() does not seem to do this (it's done
beforehand by whoever fills sys.argv[])

Thanks.

Giovanni Bajo






More information about the Python-list mailing list