如何扩展 h5py 以便我可以访问 hdf5 文件中的数据?
我有一个小的 python 程序,它使用 h5py 模块创建一个 hdf5 文件。我想编写一个 python 模块来处理 hdf5 文件中的数据。我怎么能这么做呢?
更具体地说,我可以将 numpy 数组设置为 PyArrayObject 并使用 PyArg_ParseTuple 读取它们。这样,当我编写 python 模块时,我可以从 numpy 数组中读取元素。如何读取 hdf5 文件以便访问各个元素?
更新:感谢下面的回答。我需要从 C 而不是从 Python 读取 hdf5 文件 - 我知道该怎么做。例如:
import h5py as t
import numpy as np
f=t.File('\tmp\tmp.h5', 'w')
#this file is 2+GB
ofmat=np.load('offsetmatrix.npy')
f['FileDataset']=ofmat
f.close()
现在我有一个名为“\tmp\tmp.h5”的 hdf5 文件。我需要做的是使用 C (而不是 python)从 hdf5 文件中读取各个数组元素,以便我可以对这些元素执行某些操作。 这展示了如何扩展 numpy 数组。如何扩展hdf5?
编辑:语法
I have a small python program which creates a hdf5 file using the h5py module. I want to write a python module to work on the data from the hdf5 file. How could I do that?
More specifically, I can set the numpy arrays to PyArrayObject and read them using PyArg_ParseTuple. This way, I can read elements from the numpy array when I am writing a python module. How to read hdf5 files so that I can access individual elements?
Update: Thanks for the answers below. I need to read hdf5 file from C and not from Python- I know how to do that. For example:
import h5py as t
import numpy as np
f=t.File('\tmp\tmp.h5', 'w')
#this file is 2+GB
ofmat=np.load('offsetmatrix.npy')
f['FileDataset']=ofmat
f.close()
Now I have a hdf5 file called '\tmp\tmp.h5'. What I need to do is read the individual array elements from the hdf5 file using C (and not python) so that I can do something with those elements. This shows how to extend numpy arrays. How to extend hdf5?
Edit: Grammar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您实际上并不需要 HDF5 的特定结构,但您只需要速度和跨平台兼容性,我建议您查看 PyTables。它具有读取和写入 Numpy 数组的内置功能。
If you don't actually need a particular structure of HDF5, but you just need the speed and cross-platform compatibility, I'd recommend taking a look at PyTables. It has the built-in ability to read and write Numpy arrays.
h5py 为您提供了一个直接接口,用于读取/写入和操作 hdf5 文件中存储的数据。你看过文档吗?
http://docs.h5py.org/
我建议从这些开始。这些有非常清晰的示例说明如何进行简单的数据访问。如果 h5py 中的方法未涵盖您尝试执行的特定操作,您能否更具体地描述您所需的用法?
h5py gives you a direct interface for reading/writing and manipulating data stored in an hdf5 file. Have you looked at the docs?
http://docs.h5py.org/
I advise starting with these. These have pretty clear examples of how to do simple data access. If there are specific things that you are trying to do that aren't covered by the methods in h5py, could you please give a more specific description of your desired usage?