Last active
November 28, 2020 16:35
-
-
Save TinyPoro/edb3b5d0123dd0ed8f9004be4e297b45 to your computer and use it in GitHub Desktop.
Parse multi form in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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