八度音程
>> A = [1,2,3;4,5,6];
>> B = [1,2,3,4];
>> save -hdf5 abc.h5 A B
在 Ipython 中
In [138]: import h5py
In [139]: f = h5py.File('abc.h5')
In [140]: list(f.keys())
Out[140]: ['A', 'B']
In [141]: list(f['A'].keys())
Out[141]: ['type', 'value']
In [142]: f['A']['value']
Out[142]: <HDF5 dataset "value": shape (3, 2), type "<f8">
In [143]: A = f['A']['value'][:]
In [144]: A
Out[144]:
array([[ 1., 4.],
[ 2., 5.],
[ 3., 6.]])
另请参阅侧栏中的链接。
基本上,这是找到所需数据集,然后按照中所述加载它的问题http://docs.h5py.org/en/latest/high/dataset.html#reading-writing-data
https://pypi.python.org/pypi/hdf5storage/0.1.14- 这个包有MATLAB MAT v7.3 file support
。我还没用过。
In [550]: import hdf5storage
In [560]: bar = hdf5storage.read(filename='abc.h5')
In [561]: bar
Out[561]:
array([ ([(b'matrix', [[ 1., 4.], [ 2., 5.], [ 3., 6.]])], [(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])])],
dtype=[('A', [('type', 'S7'), ('value', '<f8', (3, 2))], (1,)), ('B', [('type', 'S7'), ('value', '<f8', (4, 1))], (1,))])
因此,该文件已作为结构化数组加载,具有形状 (1,) 和 2 个字段“A”和“B”(2 个变量名称)。每个都有一个“类型”和“值”字段。
In [565]: bar['A']['value']
Out[565]:
array([[[[ 1., 4.],
[ 2., 5.],
[ 3., 6.]]]])
或者使用它的loadmat
:
In [570]: out = hdf5storage.loadmat('abc.h5',appendmat=False)
In [571]: out
Out[571]:
{'A': array([(b'matrix', [[ 1., 4.], [ 2., 5.], [ 3., 6.]])],
dtype=[('type', 'S7'), ('value', '<f8', (3, 2))]),
'B': array([(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])],
dtype=[('type', 'S7'), ('value', '<f8', (4, 1))])}
out
是一本字典:
In [572]: out['B']['value']
Out[572]:
array([[[ 1.],
[ 2.],
[ 3.],
[ 4.]]])
对于读取简单的 MATLAB 文件来说,这并没有增加太多。它可能会添加更多的细胞或结构。但对于编写 MATLAB 兼容文件来说,它应该是一个很大的帮助(尽管对于编写一个可以坚持scipy.io.savemat
).