regex - How to parse random key value pair string in Python? -
what's efficient way parse string?
accountsid=ac03e13571d5434152f33553f6&messagesid=sm16bbbc0a82847e61200a59d82cc1732b&body=https%3a%2f%2fwww.youtube.com%2fwatch%3fv%3dztwuf1mmama%26feature%3dyoutube_gdata_player&tozip=92707&tocity=garden+grove&fromstate=ca&tostate=ca&smssid=sm16bbbc0a82847e61200a59d82cc1732b&to=%2b17147866508&tocountry=us&fromcountry=us&smsmessagesid=sm16bbbc0a82847e61200a59d82cc1732b&apiversion=2010-04-01&fromcity=santa+ana&smsstatus=received&nummedia=0&from=%2b17145420218&fromzip=92704
use urlparse.parse_qs()
parse url-encoded key-value pairs.
demo:
>>> import urlparse >>> inputstr = 'accountsid=ac03e13571d5434152f33553f6&messagesid=sm16bbbc0a82847e61200a59d82cc1732b&body=https%3a%2f%2fwww.youtube.com%2fwatch%3fv%3dztwuf1mmama%26feature%3dyoutube_gdata_player&tozip=92707&tocity=garden+grove&fromstate=ca&tostate=ca&smssid=sm16bbbc0a82847e61200a59d82cc1732b&to=%2b17147866508&tocountry=us&fromcountry=us&smsmessagesid=sm16bbbc0a82847e61200a59d82cc1732b&apiversion=2010-04-01&fromcity=santa+ana&smsstatus=received&nummedia=0&from=%2b17145420218&fromzip=92704' >>> pprint import pprint >>> pprint(urlparse.parse_qs(inputstr)) {'accountsid': ['ac03e13571d5434152f33553f6'], 'apiversion': ['2010-04-01'], 'body': ['https://www.youtube.com/watch?v=ztwuf1mmama&feature=youtube_gdata_player'], 'from': ['+17145420218'], 'fromcity': ['santa ana'], 'fromcountry': ['us'], 'fromstate': ['ca'], 'fromzip': ['92704'], 'messagesid': ['sm16bbbc0a82847e61200a59d82cc1732b'], 'nummedia': ['0'], 'smsmessagesid': ['sm16bbbc0a82847e61200a59d82cc1732b'], 'smssid': ['sm16bbbc0a82847e61200a59d82cc1732b'], 'smsstatus': ['received'], 'to': ['+17147866508'], 'tocity': ['garden grove'], 'tocountry': ['us'], 'tostate': ['ca'], 'tozip': ['92707']}
Comments
Post a Comment