3

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]

2 Answers 2

12

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]}
Sign up to request clarification or add additional context in comments.

Comments

1
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)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.