split parameter line with quotes

teddyber teddyber at gmail.com
Fri Jan 11 20:47:07 CET 2008


On 11 jan, 20:28, Nanjundi <nanju... at gmail.com> wrote:
> On Jan 11, 1:50 pm, teddyber <teddy... at gmail.com> wrote:
>
> > Hello,
>
> > 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...
>
> > thanks
>
> This is unconventional and using eval is not SAFE too.>>> s = 'qop="auth,auth-int,auth-conf",cipher="rc4-40,rc4-56,rc4,des,3des",maxbuf=1024,charset="utf-8",algorithm="md5-sess"'
> >>> d = eval(' dict(%s)' % s)
> >>> d.items()
thanks for that. The problem is i don't have charset="utf-8" but
charset=utf-8. Sometimes " sometimes not!
>
> [('algorithm', 'md5-sess'), ('maxbuf', 1024), ('charset', 'utf-8'),
> ('cipher', 'rc4-40,rc4-56,rc4,des,3des'), ('qop', 'auth,auth-int,auth-
> conf')]>>> for k,v in d.iteritems(): print k, '=', v
>
> ...
> algorithm = md5-sess
> maxbuf = 1024
> charset = utf-8
> cipher = rc4-40,rc4-56,rc4,des,3des
> qop = auth,auth-int,auth-conf
>
> For safe eval, take a look athttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469
>
> -N




More information about the Python-list mailing list