在八度
>> 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
)。
我們在之前的SO問題中探討了這一點。但與此同時,探索文件的數據結構。看看'f.keys()'(在py3中添加'list(...)')。您可能需要完成多個層次。另請閱讀使用'h5py'的基礎知識。 – hpaulj
從側邊欄:https://stackoverflow.com/questions/19310808/how-to-read-a-v7-3-mat-file-via-h5py?rq=1和https://stackoverflow.com/questions/27670149 /讀MATLAB的v7-3文件 - 進入 - 蟒一覽的-numpy的陣列-通h5py – hpaulj