python query_string转dict
PHP中文网
PHP中文网 2017-04-18 09:12:47
0
5
526

有如下字query符串
s = "data=Ap/8ime+SAJmU=&a=dfasfds+fsdsaf&c=fas1mk342412"
需要转成dict
{'data': 'Ap/8ime+SAJmU=', 'a': 'dfasfds+fsdsaf', 'c': 'fas1mk342412'}

试过dict(urlparse.parse_qsl(s)),然而'+'会转成空格。
然后试过split('&'),再split('='),然而'data'value中包含'='。汗啊-_-!!
求一个高效的方法。

PHP中文网
PHP中文网

认证0级讲师

reply all(5)
黄舟

That’s it

>>> s="data=Ap/8ime+SAJmU=&a=dfasfds+fsdsaf&c=fas1mk342412"
>>> dict([x.split('=',1) for x in s.split('&')])
 {'a': 'dfasfds+fsdsaf', 'c': 'fas1mk342412', 'data': 'Ap/8ime+SAJmU='}
伊谢尔伦

However, the plus sign should be converted into a space!

Just use parse_qsl

迷茫
List=map(list,zip(*[i.split('=',1) for i in s.split('&')]))

dict(zip(List[0],List[1]))

My level can only go so far

左手右手慢动作
import re
s = "data=Ap/8ime+SAJmU=&a=dfasfds+fsdsaf&c=fas1mk342412"
d = dict(re.findall(r"([^=&]+)=([^=&]*)",s))
print d
小葫芦

Two o’clock

  1. As @hsfzxjy said, + This is to convert the blank space

  2. If you want to convert it into a dictionary, just use parse_qs at the beginning. It will be converted directly into dict. There is no need to manually transfer the list to it


Questions I answered: Python-QA

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template