擅长:python、mysql、java
<p>从这里开始:</p>
<pre><code>from collections import defaultdict
import re
import pprint
def convert(d):
result = defaultdict(dict)
for key, value in d.items():
main, sub = re.match(r'(\w+)\[(\w+)\]', key).groups()
result[main][sub] = value
result = dict(result) # just for display really
pprint.pprint(result)
convert({
'key1[subkey0]': 'value 0',
'key1[subkey1]': 'value 1',
'key2[subkey0]': 'value 2',
'key2[subkey1]': 'value 3',
})
</code></pre>
<p>输出:</p>
^{pr2}$