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]
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]}
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With