Reputation: 17412
I've a string
a = "sequence=1,2,3,4&counter=3,4,5,6"
How do I convert it into a list,ie
sequence = [1,2,3,4]
counter = [3,4,5,6]
Upvotes: 3
Views: 241
Reputation: 581
import re
items = "sequence=1,2,3,4&counter=3,4,5,6".split('&')
pattern = re.compile(r'\d+')
for i in items:
print [int(i) for i in re.findall(pattern, i)]
Upvotes: 1
Reputation: 369444
Use urlparse.parse_qs
(urllib.parse.parse_qs
in Python 3.x) to parse query string:
>>> import urlparse
>>> a = "sequence=1,2,3,4&counter=3,4,5,6"
>>> {key: [int(x) for x in value[0].split(',')]
... for key, value in urlparse.parse_qs(a).items()}
{'counter': [3, 4, 5, 6], 'sequence': [1, 2, 3, 4]}
Upvotes: 12