python - 如果 HDF5 组/表不存在,则创建它

标签 python hdf5 pytables

我正在使用 PyTables python 包构建 HDF5 文件。该文件每天都会使用最新的报价数据进行更新。我想创建两个组 - QuotesTrades 以及不同 future 到期日的表格。我想检查组 Quotes 是否存在,如果不存在则创建它。在 PyTables 中执行此操作的最佳方法是什么?

这是我现在所在位置的代码片段:

hdf_repos_filters = tables.Filters(complevel=1, complib='zlib')
for instrument in instruments:
    if options.verbose:
    hdf_file = os.path.join(dest_path, "{}.h5".format(instrument))
    store = tables.open_file(hdf_file, mode='a', filters=hdf_repos_filters)
    # This is where I want to check whether the group "Quotes" and "Trades" exist and if not create it

最佳答案

Kapil 的方向是正确的,因为您想要使用 __contains__方法,尽管因为它是双下划线方法,所以不打算直接调用,而是通过备用接口(interface)调用。在这种情况下,该接口(interface)是 in 。所以要检查一个文件hdf_file包含一个组"Quotes"你可以运行:

with tables.open_file(hdf_file) as store:
    if "/Quotes" in store:
       print(f"Quotes already exists in the file {hdf_file}")

关于python - 如果 HDF5 组/表不存在,则创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102721/

相关文章:

python - 没有名为 slack 的模块

python - BeautifulSoup 理解相对 URL 吗?

anaconda - HDF5是否支持并发读取或写入不同文件?

python - 如何将 'date' 类型的值添加到 pytable?

python - 如何检查具体方法是否遵守抽象方法的类型提示

Python Pandas 计数

python - "Group By"HDFStore 中大数据的多个列

julia - 使用 Julia HDF5 以 numpy 轴顺序读取 HDF5 数据

python - 类型错误 : read_hdf() takes exactly 2 arguments (1 given)

python - 使用属性从 H5 文件中过滤 HDF 数据集