-5
app=related_query&type=json&src=sm_rec&count=20&sc=structure_web_info&uid=d4a048cc49a11ad0f98d83608c44b554%7C26858319344%7C%7C1508669758&uuid=d4a048cc49a11ad0f98d83608c44b554%7C26858319344%7C%7C1508669758&query=%E5%8E%86%E5%8F%B2%E6%B4%BB%E4%B8%A4%E7%99%BE%E5%B2%81%E4%BB%A5%E4%B8%8A%E7%9A%84&url=http%3A%2F%2Fwww.360doc.cn%2Farticle%2F150887_537727792.html&from=ucframe&title=%E5%8E%86%E5%8F%B2%E4%B8%8A%E6%9C%80%E9%95%BF%E5%AF%BF%E7%9A%84%E4%BA%BA%2C%E6%B4%BB%E5%88%B0%E4%B8%A4%E7%99%BE%E5%B2%81%E7%9A%84%E5%A5%A5%E7%A7%98&weini_sn=0

In [38]: line[str].split('&')
Out[38]:
['app=related_query',
 'type=json',
 'src=sm_rec',
 'count=20',
 'sc=structure_web_info',
 'uid=d4a048cc49a11ad0f98d83608c44b554%7C26858319344%7C%7C1508669758',
 'uuid=d4a048cc49a11ad0f98d83608c44b554%7C26858319344%7C%7C1508669758',
 'query=%E5%8E%86%E5%8F%B2%E6%B4%BB%E4%B8%A4%E7%99%BE%E5%B2%81%E4%BB%A5%E4%B8%8A%E7%9A%84',
 'url=http%3A%2F%2Fwww.360doc.cn%2Farticle%2F150887_537727792.html',
 'from=ucframe',
 'title=%E5%8E%86%E5%8F%B2%E4%B8%8A%E6%9C%80%E9%95%BF%E5%AF%BF%E7%9A%84%E4%BA%BA%2C%E6%B4%BB%E5%88%B0%E4%B8%A4%E7%99%BE%E5%B2%81%E7%9A%84%E5%A5%A5%E7%A7%98',
 'weini_sn=0',
 'bucket=']

How to transform the original string into a dictionary which has key value. Thanks.

yanachen
  • 3,401
  • 8
  • 32
  • 64

1 Answers1

0

Don't parse this manually; Python has the urllib.parse.parse_qs function that does exactly what you want.

from urllib.parse import parse_qs
parse_qs(line)

In Python 2 this is in the urlparse module:

from urlparse import parse_qs
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Since Python 3 has been available for almost nine years now I tend to assume people are using it unless they say otherwise. – Daniel Roseman Nov 16 '17 at 13:24