Django,Python:是否有一种简单的方法可以将PHP样式的带括号的POST键转换为多维字典?


问题内容

具体来说,我有一个表单,该表单调用Django服务(使用Piston编写,但我认为不相关),并通过POST发送如下内容:

edu_type[3][name] => a
edu_type[3][spec] => b
edu_type[3][start_year] => c
edu_type[3][end_year] => d
edu_type[4][0][name] => Cisco
edu_type[4][0][spec] => CCNA
edu_type[4][0][start_year] => 2002
edu_type[4][0][end_year] => 2003
edu_type[4][1][name] => fiju
edu_type[4][1][spec] => briju
edu_type[4][1][start_year] => 1234
edu_type[4][1][end_year] => 5678

我想在Python端进行处理,以得到如下内容:

edu_type = {
    '3' : { 'name' : 'a', 'spec' : 'b', 'start_year' : 'c', end_year : 'd' },
    '4' : {
        '0' : { 'name' : 'Cisco', 'spec' : 'CCNA', 'start_year' : '2002', 'end_year' : '2003' },
        '1' : { 'name' : 'fiju', 'spec' : 'briju', 'start_year' : '1234', 'end_year' : '5678' },
    },
}

有任何想法吗?谢谢!


问题答案:

我在python中做了一个小解析器来处理多维字典,您可以在https://github.com/bernii/querystring-
parser中
找到它