I need an array for saving values, but I also want to edit some of the values in the array anytime later.
I created an array with some random values and save it to disk. I can read it. Than I want to update it, an array slice with the value '23'. When I read it again it looks like it doesn't change.
How can I update this values?
import numpy as np
import h5py
x, y = 100,20
# create
a = np.random.random(size=(x, y))
h5f = h5py.File('data.h5', 'w')
h5f.create_dataset('dataset_1', data=a)
print a[1][0:5] # [ 0.77474947 0.3618912 0.16000164 0.47827977 0.93955235]
h5f.close()
# read
h5f = h5py.File('data.h5','r')
b = h5f['dataset_1'][:]
print b[1][0:5] #[ 0.77474947 0.3618912 0.16000164 0.47827977 0.93955235]
h5f.close()
# update
h5f = h5py.File('data.h5', 'r+')
b = h5f['dataset_1'][:]
b[1][0:5] = 23
print b[1][0:5] #[ 23. 23. 23. 23. 23.]
h5f.close()
# read again
h5f = h5py.File('data.h5','r')
b = h5f['dataset_1'][:]
print b[1][0:5] #[ 0.77474947 0.3618912 0.16000164 0.47827977 0.93955235]
h5f.close()