python多维字典_urlencode python中的多维字典

  • Post author:
  • Post category:python


好的人。我自己实现了:import urllib

def recursive_urlencode(d):

“””URL-encode a multidimensional dictionary.

>>> data = {‘a’: ‘b&c’, ‘d’: {‘e’: {‘f&g’: ‘h*i’}}, ‘j’: ‘k’}

>>> recursive_urlencode(data)

u’a=b%26c&j=k&d[e][f%26g]=h%2Ai’

“””

def recursion(d, base=[]):

pairs = []

for key, value in d.items():

new_base = base + [key]

if hasattr(value, ‘values’):

pairs += recursion(value, new_base)

else:

new_pair = None

if len(new_base) > 1:

first = urllib.quote(new_base.pop(0))

rest = map(lambda x: urllib.quote(x), new_base)

new_pair = “%s[%s]=%s” % (first, ‘][‘.join(rest), urllib.quote(unicode(value)))

else:

new_pair = “%s=%s” % (urllib.quote(unicode(key)), urllib.quote(unicode(value)))

pairs.append(new_pair)

return pairs

return ‘&’.join(recursion(d))

if __name__ == “__main__”:

import doctest

doctest.testmod()

不过,我有兴趣知道是否有更好的方法来做到这一点。我不敢相信Python的标准库没有实现这一点。