p**z 发帖数: 65 | 1 不把笔记翻译成中文了,就直接贴了。
Example Python code below for creating the HDF5 file. Note uncompressed or '
gzip' compression type can be understood by both Matlab and HDFView.
from __future__ import division, print_function
import h5py
import numpy as np
data = np.array([('John', 35, 160.5), ('Mary', 20, 150)], dtype= [('Name', '
a10'), ('Age' ,'i'), ('Weight', 'f')])
##alternative:
#data = np.array([('John', 35, 160.5), ('Mary', 20, 150)], dtype = {'names':
['Name','Age','Weight'], 'formats':['a10','i','f']})
f = h5py.File(r'C:hdf5test.h5', 'w')
f.create_dataset('testdata', data = data, compression = 'gzip')
# f['testdata'] = data # alternative for uncompressed
f.close()
print('Data saved to HDF5 file.n')
f = h5py.File(r'C:hdf5test.h5', 'r')
rddata = f['testdata']
print('Data read-back:')
print(rddata[:])
Result:
Data saved to HDF5 file.
Data read-back:
[('John', 35, 160.5) ('Mary', 20, 150.0)]
>>>
Viewing test.h5 in HDFView:
Matlab code for reading the file created above:
clc; clear all;
fn = 'C:hdf5test.h5';
data = hdf5read(fn, 'testdata');
fprintf('Data read back from HDF5 file:nn')
for nrecord = 1:2
row = data(nrecord).Data;
fprintf('%st%dt%fn', row{1}.Data, row{2}, row{3});
end
Result:
Data read back from HDF5 file:
John 35 160.500000
Mary 20 150.000000
>> |
|