在 Python 中打开 .h5 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46851600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:53:12  来源:igfitidea点击:

Open .h5 file in Python

pythonpandash5py

提问by python_enthusiast

I am trying to read a h5 file in Python.

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

The file can be found in this linkand it is called 'vstoxx_data_31032014.h5'. The code I am trying to run is from the book Python for Finance, by Yves Hilpisch and goes like this:

该文件可在此链接中找到,名为“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()

I am getting the following error:

我收到以下错误:

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'

where I have substituted my working directory for 'path.../' for the purpose of this question.

出于这个问题的目的,我已将我的工作目录替换为“路径.../”。

Does anyone know where this error might be coming from?

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

采纳答案by DavidG

In order to open a HDF5 file with the h5pymodule you can use h5py.File(filename). The documentation can be found here.

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

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()