In [1]:
from __future__ import print_function
import json
In [2]:
data = {
'one': 1,
'two': 2,
'list': [1, 2, 3, 4],
'matrix': [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
'float_list': [1.0, 2.0],
'dict': {
'one': 4,
'two': 5,
'dict': {
'list': [0, 1, 2, 8]
}
}
}
json.dump(data, open('data.json', 'w'), indent=2, sort_keys=True)
In [4]:
for kk, vv in data.items():
print(' The key is:', kk)
print('The value is:', vv)
print()
Iterate over the elements of the matrix
In [5]:
for row in data['matrix']:
for el in row:
print(el, end=' ')
print()
In [6]:
data['matrix'][0][0] += 1000
print(data['matrix'])
In [7]:
data['dict']['dict']
Out[7]:
In [8]:
data['dict']['dict']['list'][2]
Out[8]:
In [9]:
data_2 = json.load(open('data.json', 'r'))
print(json.dumps(data, indent=2, sort_keys=True))
print(json.dumps(data_2, indent=2, sort_keys=True))
In [ ]:
In [ ]: