【问题标题】:Using pandas and PyTables (3.1.1) at the same time, re-opening an already open file同时使用pandas和PyTables(3.1.1),重新打开一个已经打开的文件
【发布时间】:2014-07-09 21:58:11
【问题描述】:

我同时使用 pandas 和 pytables (3.1.1)。问题是我已经用 pytables 打开了一个 HDF5 文件,当我尝试用 pandas 创建一个新的 HDF5Store 时

hdf5store = HDFStore(...)

我收到以下错误:

  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/pandas/io/pytables.py", line 281, in __init__
    self.open(mode=mode, warn=False)
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/pandas/io/pytables.py", line 387, in open
    self._handle = h5_open(self._path, self._mode)
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/pandas/io/pytables.py", line 154, in h5_open
    return tables.openFile(path, mode)
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/tables/_past.py", line 35, in oldfunc
    return obj(*args, **kwargs)
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/tables/file.py", line 296, in open_file
    _FILE_OPEN_POLICY))
ValueError: The file '/tmp/temp_folder_for_pypet_tests/experiments/tests/HDF5/merge1.hdf5' is already opened.  Please close it before reopening.  HDF5 v.1.8.4-patch1, FILE_OPEN_POLICY = 'strict'

我如何告诉 pandas 不要重新打开文件,而只是使用已经给定的文件句柄?

非常感谢!

【问题讨论】:

    标签: python pandas hdf5 pytables


    【解决方案1】:

    您需要显式传递mode='r' 以强制以只读模式打开。默认以mode='a'(追加模式)打开。

    最近版本的 PyTables 变得更加严格,只允许在写入模式下打开文件一次,即使跨多个进程/线程也是如此。这是为了避免可能的文件损坏。

    如果您有最新的 HDF5 库版本,您可以在只读模式下多次打开该文件(否则您将获得不同的异常)。

    【讨论】:

    • 是的,我知道,但我必须将 pandas 数据写入 hdf5 文件(所以我需要在模式 'w' 下使用它)。我不想让 pandas 重新打开文件,而只是使用我已有的文件句柄。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多