python-同时使用pandas和PyTables(3.1.1),重新打开一个已经打开的文件
作者:互联网
我同时使用了pandas和pytables(3.1.1).问题是我已经用pytables打开了HDF5文件,并且当我尝试用熊猫创建新的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'
如何告诉熊猫不要重新打开文件,而只是使用已经给定的文件句柄?
非常感谢!
解决方法:
您需要显式传递mode =’r’才能以只读模式强制打开.默认为在mode =’a'(追加模式)下打开.
PyTables的最新版本变得更加严格,仅允许一次以写入模式打开文件,即使跨多个进程/线程也是如此.这是为了避免文件损坏.
如果您具有最新的HDF5库版本,则可以以只读模式多次打开文件(否则,将获得其他异常).
标签:pandas,hdf5,pytables,python 来源: https://codeday.me/bug/20191029/1960721.html