如何将元素添加到json列表 - python

时间:2015-05-20 13:00:59

标签: python json

从此

bau_nr_komplett   summe
              1   0     
              2   0     
              3   5230+22650+52500+12200     
              4   0     
              5   0     
              6   0     
            325   22650+52500+12200     
          325.0   12200     
          325.1   52500     
        325.0.0   12200   

我会得到:

SELECT pt.bau_nr_komplett, (
 SELECT 
  SUM(IF(m1.menge IS NULL,0,m1.menge*p1.preis_vertragskosten*m1.baufortschritt/100*(p1.projekt_id=1325)))
 FROM positionstyp pt1
 LEFT JOIN projektposition p1 ON ( p1.positionstyp_id = pt1.id ) 
 LEFT JOIN menge m1 ON ( m1.projektposition_id = p1.id ) 
 WHERE pt1.bau_nr_komplett LIKE CONCAT( pt.bau_nr_komplett,  "%" ) 
 ) as summe
FROM positionstyp pt
LEFT JOIN projektposition p ON ( p.positionstyp_id = pt.id ) 
LEFT JOIN menge m ON ( m.projektposition_id = p.id ) 
GROUP BY pt.id

我想将data = json.loads(urlopen('someurl').read()) 添加到{'list': [{'a':'1'}]}

知道怎么做吗?

3 个答案:

答案 0 :(得分:22)

我会这样做:

data["list"].append({'b':'2'})

所以,您只需将对象添加到"数据"

中的列表中

答案 1 :(得分:7)

使用append()将元素添加到列表中:

>>> data = {'list': [{'a':'1'}]}
>>> data['list'].append({'b':'2'})
>>> data
{'list': [{'a': '1'}, {'b': '2'}]}

如果要将元素添加到列表中的特定位置(即开头),请改用insert()

>>> data['list'].insert(0, {'b':'2'})
>>> data
{'list': [{'b': '2'}, {'a': '1'}]}

执行此操作后,您可以从修改后的字典中再次组装JSON:

>>> json.dumps(data)
'{"list": [{"b": "2"}, {"a": "1"}]}'

答案 2 :(得分:0)

Keys

{" dict":[{" a":" none"," b":" none& #34;," c":"无"}]}

import json

myDict = {'dict': [{'a': 'none', 'b': 'none', 'c': 'none'}]}
test = json.dumps(myDict)
print(test)

{" dict":[{" a":" none"," b":" none& #34;," c":"无"},{" a":" aaaa"," b" :" aaaa"," c":" aaaa"}]}