我是一个字符串
a = "sequence=1,2,3,4&counter=3,4,5,6"
如何将其转换为列表,即
sequence = [1,2,3,4]
counter = [3,4,5,6]
答案 0 :(得分:12)
使用urlparse.parse_qs
(Python 3.x中的urllib.parse.parse_qs
)来解析查询字符串:
>>> 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]}
答案 1 :(得分: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)]