我正在使用 PyTables python 包构建 HDF5 文件。该文件每天都会使用最新的报价数据进行更新。我想创建两个组 - Quotes
和 Trades
以及不同 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/