在2元组的python zip()列表上使用urllib.parse.urlencode()

问题描述:

我正在尝试使用urllib.parse.urlencode()为获取请求生成编码的url。为此,我需要使用带有2元组列表的urllib.parse.urlencode()。我需要生成一个动态列表,因为获取请求是基于输入的位置。这是一个mapquest API获取请求。我使用zip()创建了动态列表和2元组列表,但urllib.parse.urlencode()不适用于2元组列表。请让我知道我做错了什么,或者如果有另一种方式来做到这一点。谢谢。在2元组的python zip()列表上使用urllib.parse.urlencode()

import urllib.parse 

add = '' 
tolist=[] 
newlist = [] 
locations = ['austin, tx', 'dallas, tx', 'denver, co', 'houston, tx','irving, tx', '3'] 
for item in range(2,len(locations)-1): 
    tolist.append('to') 
    newlist.append(locations[item]) 

print(locations[0]) 
print('tolist', tolist) 
print('newlist', newlist) 
zipped=zip(tolist, newlist) 

add = add + urllib.parse.urlencode(zipped) 

print() 
parselist=[('to', 'denver, co'),('to', 'houston, tx'),('to', 'irving, tx')] 
add = add + urllib.parse.urlencode(parselist) #this works 
print('add', add) 

嗨,再次,对不起,我发现了问题,我现在得到它的工作。非常感谢。

变化

##add = add + urllib.parse.urlencode(ziplist) #this does not work

要:

add = add + urllib.parse.urlencode(list(zipped)) `

+0

嗨,这是注释掉的目的,因为它不工作。 – Lynn