urlencode an array of values

后端 未结 5 1675
半阙折子戏
半阙折子戏 2020-12-13 08:25

I\'m trying to urlencode an dictionary in python with urllib.urlencode. The problem is, I have to encode an array.

The result needs to be:

criterias%         


        
5条回答
  •  一个人的身影
    2020-12-13 09:11

    The solution is far simpler than the ones listed above.

    >>> import urllib
    >>> params = {'criterias[]': ['member', 'issue']}
    >>> 
    >>> print urllib.urlencode(params, True)
    criterias%5B%5D=member&criterias%5B%5D=issue
    

    Note the True. See http://docs.python.org/library/urllib.html#urllib.urlencode the doseq variable.

    As a side note, you do not need the [] for it to work as an array (which is why urllib does not include it). This means that you do not not need to add the [] to all your array keys.

提交回复
热议问题