用引号拆分参数行 [英] split parameter line with quotes
问题描述
您好,
首先我是蟒蛇的新手(但我在互联网上搜索我发誓)。
我正在寻找一些将字符串拆分为一对列表的方法
''key = value''。这段代码应该能够处理这个特殊的
示例字符串:
qop =" auth,auth-int,auth-conf",cipher =" ; rc4-40,rc4-56,rc4,des,
3des",maxbuf = 1024,charset = utf-8,algorithm = md5-sess
$ b $我知道我可以用一些正则表达式来做到这一点(我现在正在努力学习
那个)但是如果有其他的方式...
谢谢
1月11日下午1点50分,泰晤士<泰铢...... @ gmail.com写道:
< blockquote class =post_quotes>
您好,
首先我是蟒蛇的新手(但是我发誓在互联网上搜索)。
$ b $我正在寻找一种方法将一个字符串拆分成一对列表
''key = value''。这段代码应该能够处理这个特殊的
示例字符串:
qop =" auth,auth-int,auth-conf",cipher =" ; rc4-40,rc4-56,rc4,des,
3des",maxbuf = 1024,charset = utf-8,algorithm = md5-sess
$ b $我知道我可以用一些正则表达式来做到这一点(我现在正在努力学习
那个)但是如果有其他的方式...
谢谢
这是非常规的,使用eval也不是安全的。
>> 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()
[(''algorithm'',''md5-sess''),(''maxbuf'',1024),(''charset'',''utf-8''),
(''cipher'',''rc4-40,rc4-56,rc4,des,3des''),(''qop'',''auth,auth-int,auth-
conf'')]
>>表示k,v表示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
为了安全评估,请参阅 http://aspn.activestate.com/ ASPN / Coo ... / Recipe / 364469
-N
teddyber写道:
首先我是python的新手(但是我发誓在互联网上搜索)。
我正在寻找一些分裂方法字符串成对的列表
''key = value''。这段代码应该能够处理这个特殊的
示例字符串:
qop =" auth,auth-int,auth-conf",cipher =" ; rc4-40,rc4-56,rc4,des,
3des",maxbuf = 1024,charset = utf-8,algorithm = md5-sess
$ b $我知道我可以用一些正则表达式做到这一点(我现在正在努力学习
)但是如果还有其他的方式...
看一下shlex模块。你或许可以摆弄shlex
对象并说服它在逗号上拆分。但是,说实话,如果分界逗号是空格,那么
将会更容易解析
而不是。
j
11月11日,20:28,Nanjundi< nanju ... @ gmail.comwrote:
1月11日下午1点50分,泰姬<泰迪... @ gmail.com写道:
你好,
< blockquote class =post_quotes>
首先我是python的新手(但是我发誓在互联网上搜索)。
我正在寻找一些分裂字符串的方法成对的列表
''key = value''。这段代码应该能够处理这个特殊的
示例字符串:
qop =" auth,auth-int, auth-conf",cipher =" rc4-40,rc4-56,rc4,des,
3des",maxbuf = 1024,charset = utf-8,algorithm = md5-sess
i知道我可以用一些正则表达式来做到这一点(我现在正在努力学习
)但是如果有的话其他方式......
谢谢
这是非常规的,使用eval是也不是SAFE。>> 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()
谢谢你。问题是我没有charset =" utf-8"但是
charset = utf-8。有时候有时不!
>
[(''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
...
算法= md5-sess
maxbuf = 1024 >
charset = utf-8
cipher = rc4-40,rc4-56,rc4,des,3des
qop = auth,auth-int,auth -conf
如需安全评估,请查看:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469
-N
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
On Jan 11, 1:50 pm, teddyber <teddy...@gmail.comwrote: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...
thanksThis 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()
[(''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 at http://aspn.activestate.com/ASPN/Coo.../Recipe/364469
-N
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
On 11 jan, 20:28, Nanjundi <nanju...@gmail.comwrote:On Jan 11, 1:50 pm, teddyber <teddy...@gmail.comwrote:
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
这篇关于用引号拆分参数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!