首页 > 解决方案 > 以 mmap 模式从 .npy 文件加载字典

问题描述

因此,我有许多大型 .npy 文件(每个约 600MB),其结构如下:

file = {"date": current_date, "some parameters": parameters, "data": data}
numpy.save("file.npy", file)

数据字段包含一个大数组(每个元素中都有字典!),它对文件大小的贡献最大,因此我想以 mmap 模式加载文件,否则文件打开过程(np.load()。即使我只想访问“日期”字段,item()) 也太长了。

但是,在 mmap_mode='r' 中加载文件不起作用,因为文件中有字典。

np.load("test.npy", mmap_mode='r')

引发 ValueError 异常:

ValueError: Array can't be memory-mapped: Python objects in dtype.

我还能做些什么来加快文件打开时间?我现在无法重写它们,所以不建议使用 PyTables 和其他数据库等替代解决方案。我确实意识到这里数据存储问题的解决方案是愚蠢的。

标签: pythonnumpyfile

解决方案


推荐阅读