import cgi
data = 'next=/contact/200/106260296&next=/contact/200/106260296&next=/contact/200/106260296&next=/contact/200/106260296&'
x = cgi.parse_qs(data)
next_list = x['next']
我被困在如何用 for y in x 或类似的东西来获得所有“下一个”。
有任何想法吗?
import cgi
data = 'next=/contact/200/106260296&next=/contact/200/106260296&next=/contact/200/106260296&next=/contact/200/106260296&'
x = cgi.parse_qs(data)
next_list = x['next']
for i in range(len(x['next'])) :
print x['next'][i]
您可以使用普通的旧列表切片,而不是导入另一个库:
data = 'next=/contact/200/106260296&next=/contact/200/106260296&next=/contact/200/106260296&next=/contact/200/106260296&'
next_list = [x[:-1] for x in data.split('next=')[1:]]
现在next_list
看起来像这样:['/contact/200/106260296', '/contact/200/106260296', '/contact/200/106260296', '/contact/200/106260296']
。
my_list = [j.split('/')[1:] for j in
[e.split('/contact')[1] for e in cgi.parse_qs(data)['next']]
]
然后您可以执行以下操作:
for x, y in my_list:
print x, y
输出:
200 106260296
200 106260296
200 106260296
200 106260296