在Python中打开.h5文件

时间:2017-10-20 14:43:00

标签: python pandas h5py

我正在尝试用Python读取一个h5文件。

该文件可在this link中找到,名为' vstoxx_data_31032014.h5'。我试图运行的代码来自Yves Hilpisch撰写的Python for Finance一书,如下所示:

import pandas as pd     
h5 = pd.HDFStore('path.../vstoxx_data_31032014.h5', 'r')
futures_data = h5['futures_data']  # VSTOXX futures data
options_data = h5['options_data']  # VSTOXX call option data
h5.close()

我收到以下错误:

h5 = pd.HDFStore('path.../vstoxx_data_31032014.h5', 'r')
Traceback (most recent call last):

  File "<ipython-input-692-dc4e79ec8f8b>", line 1, in <module>
    h5 = pd.HDFStore('path.../vstoxx_data_31032014.h5', 'r')

  File "C:\Users\Laura\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 466, in __init__
    self.open(mode=mode, **kwargs)

  File "C:\Users\Laura\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 637, in open
    raise IOError(str(e))

OSError: HDF5 error back trace

  File "C:\aroot\work\hdf5-1.8.15-patch1\src\H5F.c", line 604, in H5Fopen
    unable to open file
  File "C:\aroot\work\hdf5-1.8.15-patch1\src\H5Fint.c", line 1085, in H5F_open
    unable to read superblock
  File "C:\aroot\work\hdf5-1.8.15-patch1\src\H5Fsuper.c", line 277, in H5F_super_read
    file signature not found

End of HDF5 error back trace

Unable to open/create file 'path.../vstoxx_data_31032014.h5'

我将工作目录替换为&#39;路径... /&#39;为了这个问题的目的。

有谁知道这个错误可能来自哪里?

1 个答案:

答案 0 :(得分:3)

要使用h5py模块打开HDF5文件,您可以使用h5py.File(filename)。可以找到文档here

import h5py

filename = "vstoxx_data_31032014.h5"

h5 = h5py.File(filename,'r')

futures_data = h5['futures_data']  # VSTOXX futures data
options_data = h5['options_data']  # VSTOXX call option data

h5.close()