首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果HDF5组/Table不存在,则创建它

如果HDF5组/Table不存在,则创建它
EN

Stack Overflow用户
提问于 2016-05-09 01:27:22
回答 1查看 2.5K关注 0票数 3

我正在使用PyTables python包构建一个HDF5文件。该文件将每天更新最新的节拍数据。我想创建两个组- QuotesTrades,并为不同的期货到期创建表。我想检查组Quotes是否存在,如果不存在,则创建它。在PyTables中做这件事的最好方法是什么?

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-09-10 07:06:04

Kapil是正确的,因为您希望使用__contains__方法,尽管因为它是一个双下划线方法,所以不打算直接调用它,而是通过另一个接口进行调用。在本例中,接口是in。因此,要检查包含组"Quotes"的文件hdf_file,可以运行以下命令:

代码语言:javascript
复制
with tables.open_file(hdf_file) as store:
    if "/Quotes" in store:
       print(f"Quotes already exists in the file {hdf_file}")
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37102721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档