Skip to content

Instantly share code, notes, and snippets.

@TinyPoro
Last active November 28, 2020 16:35
Show Gist options
  • Save TinyPoro/edb3b5d0123dd0ed8f9004be4e297b45 to your computer and use it in GitHub Desktop.
Save TinyPoro/edb3b5d0123dd0ed8f9004be4e297b45 to your computer and use it in GitHub Desktop.
Parse multi form in Python
def parse_multi_form(form):
data = {}
for url_k in form:
v = form[url_k]
ks = []
while url_k:
if '[' in url_k:
k, r = url_k.split('[', 1)
ks.append(k)
if r[0] == ']':
ks.append('')
url_k = r.replace(']', '', 1)
else:
ks.append(url_k)
break
sub_data = data
for i, k in enumerate(ks):
if k.isdigit():
k = int(k)
if i+1 < len(ks):
if not isinstance(sub_data, dict):
break
if k in sub_data:
sub_data = sub_data[k]
else:
sub_data[k] = {}
sub_data = sub_data[k]
else:
if isinstance(sub_data, dict):
sub_data[k] = v
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment