2016-04-30 5 views
2

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) 

もう一度、申し訳ありませんが、問題が見つかりました。どうもありがとう。

答えて

1

変更

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

:それは動作しませんので、

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

+0

こんにちは、それは意図的にコメントアウトされました。 – Lynn

関連する問題