我正在使用PyTables python包构建一个HDF5文件。该文件将每天更新最新的节拍数据。我想创建两个组- Quotes
和Trades
,并为不同的期货到期创建表。我想检查组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
发布于 2018-09-10 07:06:04
Kapil是正确的,因为您希望使用__contains__
方法,尽管因为它是一个双下划线方法,所以不打算直接调用它,而是通过另一个接口进行调用。在本例中,接口是in
。因此,要检查包含组"Quotes"
的文件hdf_file
,可以运行以下命令:
with tables.open_file(hdf_file) as store:
if "/Quotes" in store:
print(f"Quotes already exists in the file {hdf_file}")
https://stackoverflow.com/questions/37102721
复制相似问题