2016-09-22 97 views
1

我想設置一個機構的urllib.request.Request()時urllib.parse.urlencode(),錯誤的結果:Python 3中,編碼字典

example_dict = {'someKey': {'anotherKey': 'someValue'}} 
urllib.parse.urlencode(example_dict) 

輸出正在失去領先{ '並且它不編碼爲%7B%27:

'someKey=%7B%27anotherKey%27%3A+%27someValue%27%7D' 

謝謝!

回答

1

您誤會了urlencode的使用。字典example_dict = {'key1': 'value1', 'key2': 'value2'}將產生key1=value1&key2=value2。它沒有用編碼大括號包裝整個事物。

+0

謝謝@Curtis,你是對的:) – camelBack