Message245431
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? |
|
Date |
User |
Action |
Args |
2015-06-17 06:48:42 | drueter@assyst.com | set | recipients:
+ drueter@assyst.com |
2015-06-17 06:48:42 | drueter@assyst.com | set | messageid: <1434523722.13.0.839663684524.issue24460@psf.upfronthosting.co.za> |
2015-06-17 06:48:42 | drueter@assyst.com | link | issue24460 messages |
2015-06-17 06:48:39 | drueter@assyst.com | create | |
|