[New-bugs-announce] [issue24460] urlencode() of dictionary not as expected
David Rueter
report at bugs.python.org
Wed Jun 17 08:48:42 CEST 2015
New submission from David Rueter:
In Python 3.4 I would like to serialize a dictionary into a URL-encoded string.
Given a dictionary like this:
>>> thisDict = {'SomeVar1': [b'abc'], 'SomeVar2': [b'def'], 'SomeVar3': [b'ghi']}
I would like to be able to return this string:
SomeVar1=abc&SomeVar2=def&SomeVar3=ghi
I thought that urllib.parse.urlencode would work for me, but it does not:
>>> print(urllib.parse.urlencode(thisDict))
SomeVar1=%5Bb%27abc%27%5D&SomeVar2=%5Bb%27def%27%5D&SomeVar3=%5Bb%27ghi%27%5D
In other words, urlencode on the dictionary is performing a URL encode on the string that is returned when the dictionary is cast to a string...and is including the square brackets (escaped) and the byte literal "b" indicator.
{'SomeVar1': [b'abc'], 'SomeVar2': [b'def'], 'SomeVar3': [b'ghi']}
I can obtain the desired string with this:
>>> '&'.join("{!s}={!s}".format(key,urllib.parse.quote_plus(str(val[0],'utf-8'))) for (key,val) in thisDict.items())
Is the behavior of urllib.parse.urlencode() on a dictionary intentional? When would the current behavior ever be useful?
Would it make sense to change the behavior of urllib.parse.urlencode such that it works as described above?
----------
components: Library (Lib)
messages: 245431
nosy: drueter at assyst.com
priority: normal
severity: normal
status: open
title: urlencode() of dictionary not as expected
type: behavior
versions: Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24460>
_______________________________________
More information about the New-bugs-announce
mailing list