I am trying to add an element to a json file in python but I am not able to do it.
This is what I tried untill now (with some variation which I deleted):
import json
data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
print 'DATA:', repr(data)
var = 2.4
data.append({'f':var})
print 'JSON', json.dumps(data)
But, what I get is:
DATA: [{'a': 'A', 'c': 3.0, 'b': (2, 4)}]
JSON [{"a": "A", "c": 3.0, "b": [2, 4]}, {"f": 2.4}]
Which is fine because I also need this to add a new row instead an element but I want to get something like this:
[{'a': 'A', 'c': 3.0, 'b': (2, 4), "f":2.4}]
How should I add the new element?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…