0

I'm using python to do a sql query that returns the result below:

print i
(123.0, 460654.05)
(234.0, 292016.43)
(1231.0, 271915.11)
(234.0, 189367.59)
(1.0, 117566.06)
(2.0, 100600.76000000001)
(3.0, 90443.32)
(4.0, 84218.40000000001)
(5.0, 82793.86)
(7.0, 77368.06)

it's a tuple.

My idea is create a Json with the result above like this:

{'items': [{'value': 123.0, 'label': '460654.05)'}, {'value': 234.0, 'label': '292016.43)'}], and so on, 'auth_token': 'mysecret'}

I tried using a for.

for i in res:
    n = {'items': [{'label': '0', 'value': '0', 'auth_token':
                    'Hues4onpDHtI2'}]}
    n['items'][0]['label'] = i[0]
    n['items'][0]['value'] = i[1]

the result was

{'items': [{'auth_token': 'mysecret', 'value': 460654.05, 'label': 123.0}]}
{'items': [{'auth_token': 'mysecret', 'value': 292016.43, 'label': 234.0}]}

Any ideas?

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
  • It would be easier to use [Python list comprehensions](http://www.pythonforbeginners.com/lists/list-comprehensions-in-python/) instead of for-loops. – Anderson Green Aug 18 '14 at 21:29
  • It would be possible to generate a dictionary using a list comprehension, as described here: http://stackoverflow.com/questions/1747817/python-create-a-dictionary-with-list-comprehension – Anderson Green Aug 18 '14 at 21:34

2 Answers2

2

You can use list comprehension like this:

[dict(zip(['label', 'value'], list(item))) for item in data]

I am assuming data is the list of tuples returned by your SQL query

Demo:

In [25]: data = [(123.0, 460654.05),
   ....: (234.0, 292016.43),
   ....: (1231.0, 271915.11),
   ....: (234.0, 189367.59),
   ....: (1.0, 117566.06),
   ....: (2.0, 100600.76000000001),
   ....: (3.0, 90443.32),
   ....: (4.0, 84218.40000000001),
   ....: (5.0, 82793.86),
   ....: (7.0, 77368.06)]
In [26]: interim_data = [dict(zip(['label', 'value'], list(item))) for item in data]
In [27]: result = {'items': interim_data, 'auth_token': 'mysecret'}

This gives:

In [28]: result
Out[28]:
{'items': [{'label': 123.0, 'value': 460654.05},
  {'label': 234.0, 'value': 292016.43},
  {'label': 1231.0, 'value': 271915.11},
  {'label': 234.0, 'value': 189367.59},
  {'label': 1.0, 'value': 117566.06},
  {'label': 2.0, 'value': 100600.76000000001},
  {'label': 3.0, 'value': 90443.32},
  {'label': 4.0, 'value': 84218.40000000001},
  {'label': 5.0, 'value': 82793.86},
  {'label': 7.0, 'value': 77368.06}],
  'auth_token': 'mysecret'
}
shaktimaan
  • 11,962
  • 2
  • 29
  • 33
1

Perhaps something like this is what you are looking for:

   results = [(123.0, 460654.05),
               (234.0, 292016.43),
               (1231.0, 271915.11),
               (234.0, 189367.59),
               (1.0, 117566.06),
               (2.0, 100600.76000000001),
               (3.0, 90443.32),
               (4.0, 84218.40000000001),
               (5.0, 82793.86),
               (7.0, 77368.06)]

    grouped = {
        'items': []
    }

    for d in results:
        grouped.get('items').append({
            'auth_token': 'mysecret',
            'label': d[0],
            'value': d[1]
        })

    print grouped
Wondercricket
  • 7,651
  • 2
  • 39
  • 58
  • I would add the auth_token into grouped. Convert it to Json with import json; print(json.dumps(grouped)); – Hendrik Aug 18 '14 at 21:39