如何在h5py复合类型中存储数组或嵌套数组?
在复合类型中存储数组或嵌套数组
针对你在h5py复合类型中存储数组、嵌套数组的需求,分两种场景给出具体解决方案:
1. 存储一维可变长度字符串数组
要让events字段支持存储类似['1739','1233-12','1234-12-06']的一维字符串列表,需要将该字段的类型定义为可变长度的utf8字符串数组,通过h5py.vlen_dtype()实现:
import h5py import numpy as np with h5py.File('12346NUL.h5', 'w') as hdfs: utf8 = h5py.string_dtype(encoding='utf-8') # 定义可变长度utf8字符串数组类型 vlen_utf8_array = h5py.vlen_dtype(utf8) dt = np.dtype([ ("id", utf8), ("events", vlen_utf8_array), # 替换原单个变长字符串类型 ("level1", utf8), ("level2", utf8), ("level3", utf8), ("lecture", np.int32), ("iso639", utf8), ("domains", utf8) ]) metadata = hdfs.create_dataset("metadata", (10,), maxshape=(None,), dtype=dt) # 直接传入列表赋值 metadata[1] = ('bpt6k619227', ['1739','1233-12','1234-12-06'], 'text', 'print monograph', 'map', 319, 'fre', 'public domains') # 验证读取 with h5py.File('12346NUL.h5', 'r') as hdfs: print(hdfs['metadata'][1]['events'])
2. 存储嵌套可变长度数组
如果要支持嵌套列表(如['1739','1233-12',['1234','1555'],'1234-12-06']),需要定义嵌套的可变长度类型——即元素本身是可变长度数组的可变长度数组:
import h5py import numpy as np with h5py.File('12346NUL_nested.h5', 'w') as hdfs: utf8 = h5py.string_dtype(encoding='utf-8') # 先定义一维可变长度utf8数组 vlen_utf8_array = h5py.vlen_dtype(utf8) # 再定义元素为一维数组的可变长度数组(嵌套结构) nested_vlen_array = h5py.vlen_dtype(vlen_utf8_array) dt = np.dtype([ ("id", utf8), ("events", nested_vlen_array), # 使用嵌套可变长度类型 ("level1", utf8), ("level2", utf8), ("level3", utf8), ("lecture", np.int32), ("iso639", utf8), ("domains", utf8) ]) metadata = hdfs.create_dataset("metadata", (10,), maxshape=(None,), dtype=dt) # 直接传入嵌套列表赋值 metadata[1] = ('bpt6k619227', ['1739','1233-12',['1234','1555'],'1234-12-06'], 'text', 'printed monograph', 'map', 319, 'fre', 'public domains') # 验证读取 with h5py.File('12346NUL_nested.h5', 'r') as hdfs: events_data = hdfs['metadata'][1]['events'] print(events_data) print(events_data[2]) # 输出子数组:['1234','1555']
注意事项
- 可变长度类型依赖HDF5的原生特性,h5py会自动处理Python列表与HDF5数组的双向转换;
- 嵌套层级可按需扩展,但过多嵌套可能影响读写性能,建议根据实际数据结构合理设计。
内容的提问来源于stack exchange,提问作者Gustave Turrell
相关产品推荐
相关产品推荐

