python - h5py 接受什么 numpy dtypes?

标签 python numpy h5py

我正在将具有 numpy 类型的 Python 字典序列化到 H5 文件。

一般情况下,代码是

for key, value in dict.items():
    if isinstance(value, str):
        f.attrs[key] = value.encode('utf-8')
    elif isinstance(value, XXXXXX):
        param_dset = f.create_dataset(key, value.shape, dtype=value.dtype)
        if not value.shape:
            # scalar
            param_dset[()] = value
        else:
            param_dset[:] = value
    elif isinstance(value, dict):
        save_dict_to_hdf5_group(f.create_group(key), value)
    else:
        raise ValueError('Cannot save type "%s" to HDF5' % type(value))

我正在努力思考要在 XXXXXX 中放入什么内容。具体来说,我可以放入任何numpy类型吗,还是H5只存储特定类型?

例如,(np.ndarray, np.int64) 是一个选择,但它会错过 float32(np.ndarray, np.generic) 是另一种选择,但 H5py 接受所有通用 numpy 类型吗?

最佳答案

来自 h5py 文档:

完全支持的类型:

Type       Precisions                                    Notes
Integer    1, 2, 4 or 8 byte, BE/LE, signed/unsigned     
Float      2, 4, 8, 12, 16 byte, BE/LE   
Complex    8 or 16 byte, BE/LE                           Stored as HDF5 struct
Compound   Arbitrary names and offsets   
Strings (fixed-length)  Any length   
Strings (variable-length)   Any length, ASCII or Unicode     
Opaque     (kind ‘V’)   Any length   
Boolean     NumPy 1-byte bool                             Stored as HDF5 enum
Array       Any supported type   
Enumeration Any NumPy integer type                        Read/write as integers
References  Region and object    
Variable length array   Any supported type                See Special Types

不支持的类型:

Type                
HDF5 “time” type     
NumPy “U” strings   
NumPy generic “O”

关于python - h5py 接受什么 numpy dtypes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587242/

相关文章:

python - 我究竟做错了什么?使用 lxml 解析 HTML

python - 在序列化器中添加一个额外的字段,但它不在模型中

python - 使用python优化图中检测循环的算法

numpy - numpy.r_ ['string integer' 的第三个字符串整数的解释,数组]

python - 对行进行排序并获取 Pandas 数据框中的列 ID

python - 如何使用 mpi4py 创建用于在节点之间传递的结构

python - 导入错误 : DLL load failed(import h5py)

python - 如何使用 NumPy ndarray 从 HDF5 数据集共享内存

python - 用 AST python 表达式中的实际值替换变量名

python - 插入许多 HDF5 数据集非常慢