标签:截取 style mdi item color strip() ict amd return
需求:
有如下URL地址,截取出 ?后面的参数,并将参数以 ‘key:value’ 的键值对形式保存。
url="http://xx.xx.xx:8000/get_account.json?page_size=20&page_index=1&user_id=456"
解法1(利用split()函数):
def getParamDict(url): paramList = url.strip().split(‘?‘)[1].split(‘&‘) paramDict = {} for item in paramList: paramDict[item.split(‘=‘)[0]] = item.split(‘=‘)[1] return paramDictif __name__ == ‘__main__‘: url = "http://xx.xx.xx:8000/get_account.json?page_size=20&page_index=1&user_id=456" print(getParamDict(url))
>>> {‘page_size‘: ‘20‘, ‘page_index‘: ‘1‘, ‘user_id‘: ‘456‘}
解法2(调用解析模块 parse)
def getParamDict(url): from urllib import parse paramDict = parse.parse_qs(parse.urlparse(url).query) return paramDict if __name__ == ‘__main__‘: url = "http://xx.xx.xx:8000/get_account.json?page_size=20&page_index=1&user_id=456" print(getParamDict(url))
>>> {‘page_size‘: [‘20‘], ‘page_index‘: [‘1‘], ‘user_id‘: [‘456‘]}
标签:截取 style mdi item color strip() ict amd return
原文地址:https://www.cnblogs.com/xiaohuboke/p/13574544.html