如何使用 Python 检索 HDF5 文件

How to use Python to retrieve HDF5 files

我有很多 .dat 格式文件,文件内部结构构造为 hdf5 结构,它有不同的数据组。现在我需要了解如何将内部数据检索为 table 以查看这些数据。我尝试使用 h5py 读取文件并将其放入 pandas 数据帧,但不幸的是有一些错误,有人可以为我提供一些示例来解决这个问题吗?

import h5py
import pandas as pa

with h5py.File("01.dat") as f:
    rdata = f.value[-1]

print rdata

This method also tried, but same error.

with h5py.File('01.dat','r') as hf:
    print('List of arrays in this file: \n', hf.keys())

下面的错误,好像无法读取 01.dat 文件。

C:\Anaconda2\python.exe C:/Users/FLU2/PycharmProjects/Dask/Dask.py
Traceback (most recent call last):
  File "C:/Users/FLU2/PycharmProjects/Dask/Dask.py", line 4, in <module>
    with h5py.File("01.dat") as f:
  File "C:\Anaconda2\lib\site-packages\h5py\_hl\files.py", line 260, in __init__
    fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
  File "C:\Anaconda2\lib\site-packages\h5py\_hl\files.py", line 114, in make_fid
    fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2584)
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2543)
  File "h5py\h5f.pyx", line 96, in h5py.h5f.create (C:\aroot\work\h5py\h5f.c:1994)
IOError: Unable to create file (Unable to open file: name = '01.dat', errno = 17, error message = 'file exists', flags = 15, o_flags = 502)

适合帮助

顺便说一下下面的内部数据结构,有元数据、结果和时间序列,在时间序列部分它有 30 个不同的组。

既然你想把它放在pandas数据框里,就用pandas.read_hdf.

http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.read_hdf.html