Reputation: 1099
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()
Upvotes: 2
Views: 4510
Reputation: 199
Append mode works for me. Create file:
fh = h5py.File('dummy.h5', 'w')
fh.create_dataset('random', data=np.reshape(np.asarray([0, 1, 2, 3]), (2, 2)))
fh.close()
Open and edit in append mode ('a', default mode)..
fh = h5py.File('dummy.h5', 'a')
print fh['random'][:]
fh['random'][0, 0] = 1337
print fh['random'][:]
fh.close()
..and check again
fh = h5py.File('dummy.h5', 'r')
print fh['random'][:]
fh.close()
Write mode ('w') seems to clear the whole file. EDIT: It's important to directly access the dataset. As an earlier answer pointed out: In your problem description you assign the contents of 'dataset_1' to b, and then you edit b.
EDIT 1: 'r+' works as well for me, the problem might be somewhere else. Maybe your way to access the data set (by index and not by name) makes the difference.
EDIT 2: Works for 2-D as well. Added some info concerning the indexing
Upvotes: 4