Reputation: 35
I have python utility i wrote that doing some WMI monitoring, the the data is written in that format
example1
CPU = [{'TS':'2013:12:03:30','CPUVALUES':['0','1','15']}]
Now i need occasionally update data that will look eventually like following
CPU = [
{'TS':'2013:12:03:30','CPUVALUES':['0','1','15']},
{'TS':'2013:14:00:30','CPUVALUES':['0','75','15']}
]
Any suggestion how to accomplish that
Please advice
Thanks
Upvotes: 0
Views: 891
Reputation: 46636
You can either read, parse and modify the file every time you need to add new data to it and that would look like:
import json
def append(filename, obj):
with open(filename, 'rb') as f:
data = json.load(f, encoding='utf-8')
data.append(obj)
with open(filename, 'wb') as f:
json.dump(data, f, encoding='utf-8')
But that could be very slow, especially if you have a large file, since you'll have to read the whole file into memory every time, deserialize it, append, serialize it again, and write it down...
If you need the extra speed, you could do a little hackery by just append the new data to the file:
import io
import json
def append(filename, data):
with open(filename, 'r+b') as f:
f.seek(-2, 2)
f.write(b',\n')
f.write(b' ' + json.dumps(data).encode('utf-8'))
f.write(b'\n]')
This code will open the file, move before the last \n]
, append ,\n
, dump the new data and add the final \n]
. You just have to be careful not to have a newline at the end of the file, because that would mess up things. But if you need to have a newline at the end, then you'll just move to -3
and at the last write append b'\n]\n'
.
Note: This code assumes that you use UNIX line endings, for Windows line endings you would have to change the moves and the \n
.
Example IPython session:
In [29]: %%file test.json
CPU = [
{"TS": "2013:12:03:30", "CPUVALUES": ["0", "1", "15"]},
{"TS": "2013:14:00:30", "CPUVALUES": ["0", "75", "15"]}
]
In [30]: !cat test.json
CPU = [
{"TS": "2013:12:03:30", "CPUVALUES": ["0", "1", "15"]},
{"TS": "2013:14:00:30", "CPUVALUES": ["0", "75", "15"]}
]
In [31]: append('test.json', {'TS':'2013:14:00:30','CPUVALUES':['0','80','15']})
In [32]: !cat test.json
CPU = [
{"TS": "2013:12:03:30", "CPUVALUES": ["0", "1", "15"]},
{"TS": "2013:14:00:30", "CPUVALUES": ["0", "75", "15"]},
{"TS": "2013:14:00:30", "CPUVALUES": ["0", "80", "15"]}
]
Upvotes: 1