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
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' },
},
}
Any ideas? Thanks!
Dottedish does something like what you want. http://pypi.python.org/pypi/dottedish. It doesn't really have a homepage but you can install it from pypi or download the source from github.
I am riding off of the previous response by Atli about using PHP's
json_encode
...A Python dict in its most basic form is syntactically identical to JSON. You could easily perform an
eval()
on a JSON structure to create a Python dict:Now, is this the best way? Probably not. But it works and doesn't resort to regular expressions, which might technically be better but is definitely not a quicker option considering the time spent debugging and troubleshooting your pattern matching.
JSON is a good format to use for interstitial data transfer.
Python also has a
json
module as part of the Standard Library. While that is more picky about the output you're parsing, it is more certainly the better way to go about it (albeit with more work involved).okay, so this is ghetto as hell, but here goes:
let's say your input is a list of tuples. say: input = [('edu_type[3][end_year]', 'd'), ...]
Note that you should only use this if you trust the source of your input, as it is nowhere near safe.
I made a little parser in python to handle multidimensional dicts, you can find it at https://github.com/bernii/querystring-parser