split parameter line with quotes

Reedick, Andrew jr9445 at ATT.COM
Fri Jan 11 16:21:06 EST 2008



> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of teddyber
> Sent: Friday, January 11, 2008 1:51 PM
> To: python-list at python.org
> Subject: split parameter line with quotes
> 
> 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...
> 

import re
s='''qop="auth,auth-int,auth-conf",cipher="rc4-40,rc4-56,rc4,des,3des",m
axbuf=1024,charset=utf-8,algorithm=md5-sess'''
print s

all = re.findall(r'(.*?)=(".*?"|[^"]*?)(,|$)', s)
print all

for i in all:
	print i[0], "=", i[1].strip('"')


Output:
qop="auth,auth-int,auth-conf",cipher="rc4-40,rc4-56,rc4,des,3des",maxbuf
=1024,charset=utf-8,algorithm=md5-sess

[
	('qop', '"auth,auth-int,auth-conf"', ','), 
	('cipher', '"rc4-40,rc4-56,rc4,des,3des"', ','), 
	('maxbuf', '1024', ','), 
	('charset', 'utf-8', ','), 
	('algorithm', 'md5-sess', '')
]

qop = auth,auth-int,auth-conf
cipher = rc4-40,rc4-56,rc4,des,3des
maxbuf = 1024
charset = utf-8
algorithm = md5-sess




*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621





More information about the Python-list mailing list