views:

202

answers:

4

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!

A: 

I don't pretend to be an expert on Python, but PHP has the json_encode function, which would be able to do this for you.

A quick Google search turned up this code, which you could try to use to serialize your data into JSON format. (Which is what you posted, if I am not mistaken.)

Atli
This has nothing to do with JSON. The end result he's looking for is a Python `dict`, which just coincidentally shares a lot of its syntax with JSON.
Will McCutchen
Ahh, ok I see. In that case I'll be backing off to quietly browse for Python tutorials, and leaving this to the Python experts ;-)
Atli
A: 

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:

>>> blob = """{
...     '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' },
...     },
... }"""
>>> edu_type = eval(blob)
>>> edu_type
{'3': {'end_year': 'd', 'start_year': 'c', 'name': 'a', 'spec': 'b'}, '4': {'1': {'end_year': '5678', 'start_year': '1234', 'name': 'fiju', 'spec': 'briju'}, '0': {'end_year': '2003', 'start_year': '2002', 'name': 'Cisco', 'spec': 'CCNA'}}}

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).

jathanism
A: 

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'), ...]

from collections import defaultdict
from re import compile

def defdict():
    return defaultdict(defdict)
edu_type = defdict()

inputs = [(x.replace('[', '["').replace(']', '"]'), y) for x, y in input]
for input in inputs:
    exec = '%s = "%s"' % input

Note that you should only use this if you trust the source of your input, as it is nowhere near safe.

andylei
+1  A: 

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.

>>> import dottedish
>>> dottedish.unflatten([('3.name', 'a'), ('3.spec', 'b')])
{'3': {'name': 'a', 'spec': 'b'}}
joeforker