解析此字符串的最有效方法是什么?
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
答案 0 :(得分:2)
使用urlparse.parse_qs()
解析URL编码的键值对。
演示:
>>> 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'
>>> from 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']}
答案 1 :(得分:2)
>>> import urlparse
>>> s = "AccountSid=AC03e13555434152f33553f6&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"
>>> urlparse.parse_qs(s)
{'Body': ['https://www.youtube.com/watch?v=ZtWUF1MmaMA&feature=youtube_gdata_player'], 'MessageSid': ['SM16bbbc0a82847e61200a59d82cc1732b'], 'FromZip': ['92704'], 'SmsStatus': ['received'], 'FromCountry': ['US'], 'FromCity': ['SANTA ANA'], 'ApiVersion': ['2010-04-01'], 'To': ['+17147866508'], 'From': ['+17145420218'], 'NumMedia': ['0'], 'ToZip': ['92707'], 'ToCountry': ['US'], 'ToState': ['CA'], 'AccountSid': ['AC03e13571dbc3a33555434152f33553f6'], 'SmsSid': ['SM16bbbc0a82847e61200a59d82cc1732b'], 'ToCity': ['GARDEN GROVE'], 'FromState': ['CA'], 'SmsMessageSid': ['SM16bbbc0a82847e61200a59d82cc1732b']}