import numpy as np
import h5py as h5
trans_detail_type = np.dtype([
('stk_code','S8'),
('trans_time','i4'),
('price','i8'),
('volumn','i8'),
('buy_order_id', 'i8'),
('sell_order_id', 'i8'),
('b_s', 'c'),
('trans_type', 'c')
])
recs = [
('SZ000001', 112233156, 90100, 30000, 12345,67890, b'B', b'F'),
('SZ000001', 112233256, 90100, 30000, 12345,67890, b'B', b'F'),
('SZ000001', 112233356, 90100, 30000, 12345,67890, b'B', b'F'),
('SZ000001', 112233456, 90100, 30000, 12345,67890, b'B', b'F'),
]
recs = np.array(recs, dtype=trans_detail_type)
print(recs)
f = h5.File('e:\\test.h5')
g_cls = f.get('trans_detail', getclass=True)
if g_cls is None or g_cls == 'default':
g = f.create_group('trans_detail')
else:
g = f['trans_detail']
ds_name = '20160929'
ds_cls = g.get(ds_name, getclass=True)
if ds_cls is None or ds_cls=='default':
pass
else:
del g[ds_name]
ds = g.create_dataset(ds_name, data=recs, compression='gzip', compression_opts=6, maxshape=(None,))
f.close()
自己写的一个利用h5py保存hdf文件的例子,留存!
最新推荐文章于 2023-11-18 08:57:47 发布