I am trying to read a .h5 file data.h5
, which has 2 datasets, 'Data' and 'metaData'. 'metaData' contains a dictionary which is 157x1, and looks like this:
Then, I am trying to write a new .h5 file, which contains 3 columns: the number, name (1st column of dictionary), and unit (last column of dictionary) of each variable in the dictionary. This is the code:
import numpy as np
import h5py as h5
hdf = h5.File('data.h5','r')
data1 = hdf.get('Data')
data2 = hdf.get('metaData')
dataset1 = np.array(data1)
dataset2 = np.array(data2)
#dictionary
hdfdic = dict(hdf['metaData'])
dic = hdfdic.get('dictionary')
dictionary = np.array(dic)
#write new h5 file
with h5.File('telemetry.h5', 'w') as var:
dt = np.dtype( [('n°', int), ('Variable name', 'S10'), ('Unit', 'S10')] )
dset = var.create_dataset( 'data', dtype=dt, shape = (len(dictionary),))
dset['n°'] = np.arange(len(dictionary))
dset['Variable name'] = [val[1] for val in dictionary[:][0][0]]
dset['Unit'] = [val[2] for val in dictionary[:][0][-1]]
data = dset[:]
print(data)
I get the error:
Traceback (most recent call last):
File "c:/Users/user/Desktop/new/code.py", line 28, in <module>
dset['Variable name'] = [val[1] for val in dictionary[:][0][0]]
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Users\user\Anaconda3\envs\py38\lib\site-packages\h5py\_hl\dataset.py", line 707, in __setitem__
for fspace in selection.broadcast(mshape):
File "C:\Users\user\Anaconda3\envs\py38\lib\site-packages\h5py\_hl\selections.py", line 299, in broadcast
raise TypeError("Can't broadcast %s -> %s" % (target_shape, self.mshape))
TypeError: Can't broadcast (4,) -> (157,)
What is the problem?