split parameter line with quotes

teddyber teddyber at gmail.com
Fri Jan 11 23:35:38 CET 2008


here's the solution i have for the moment :

	t = shlex.shlex(data)
	t.wordchars = t.wordchars + "/+.-"
	r=''
	while 1:
	    token = t.get_token()
	    if not token:
	        break
	    if not token==',': r = r+token
	    else: r = r + ' '
	self.DEBUG(r,'ok')
        for pair in r.split(' '):
            key,value=pair.split('=', 1)
                print(key+':'+value)

i know this is not perfect still but i'm coming a long way from very
bad php habits! :o)
and thanks for your help!

On 11 jan, 23:30, teddyber <teddy... at gmail.com> wrote:
> wow! that's perfect this shlex module! thanks for pointing this!
>
> On 11 jan, 20:36, Joshua Kugler <jkug... at bigfoot.com> wrote:
>
> > teddyber wrote:
> > > first i'm a newbie to python (but i searched the Internet i swear).
> > > i'm looking for some way to split up a string into a list of pairs
> > > 'key=value'. This code should be able to handle this particular
> > > example string :
>
> > > qop="auth,auth-int,auth-conf",cipher="rc4-40,rc4-56,rc4,des,
> > > 3des",maxbuf=1024,charset=utf-8,algorithm=md5-sess
>
> > > i know i can do that with some regexp (i'm currently trying to learn
> > > that) but if there's some other way...
>
> > Take a look at the shlex module.  You might be able to fiddle with the shlex
> > object and convince it to split on the commas.  But, to be honest, that
> > above would be a lot easier to parse if the dividing commas were spaces
> > instead.
>
> > j




More information about the Python-list mailing list