Django,Python:有没有一种简单的方法可以将PHP风格的括号内的POST键转换为多维dict? [英] Django, Python: Is there a simple way to convert PHP-style bracketed POST keys to multidimensional dict?

查看:182
本文介绍了Django,Python:有没有一种简单的方法可以将PHP风格的括号内的POST键转换为多维dict?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Specifically, I got a form that calls a Django service (written using Piston, but I don't think that's relevant), sending via POST something like this:

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端处理这个以获得类似这样的内容:

I would like to process this on the Python end to get something like this:

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中做了一个小解析器来处理多维dicts,你可以在 https://github.com/bernii/querystring-parser

I made a little parser in python to handle multidimensional dicts, you can find it at https://github.com/bernii/querystring-parser

这篇关于Django,Python:有没有一种简单的方法可以将PHP风格的括号内的POST键转换为多维dict?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆