如何在pandas HDF5'只读模式'文件上面写?

问题描述 投票:2回答:1

我使用pandas内置的HDF5方法存储数据。

不知何故,这些HDF5文件变成了'只读'文件,当我在写模式下打开这些文件时,我收到了很多Opening xxx in read-only mode消息,我无法写它们,这是我真正需要做的事情。

到目前为止我真的不明白的是这些文件怎么变成只读的,因为我不知道我写的一段可能导致这种行为的代码。 (我试图检查存储在HDF5中的数据是否已损坏,但我能够读取并操作它,所以它似乎工作正常)

我有两个问题:

  1. 如何将数据附加到那些“只读模式”的HDF5文件? (我可以将它们转换回写入模式或任何其他聪明的解决方案吗?)
  2. 是否有任何pandas方法默认情况下会将HDF5文件更改为“只读模式”,这样我就可以避免将这些文件转换为只读文件?

码:

提出这个问题的代码是,我用来保存我生成的输出的部分:

    with pd.HDFStore('data/observer/' + self._currency + '_' + str(ts)) as hdf:

        hdf.append(key='observers', value=df, format='table', data_columns=True)

我还使用这段代码来操作先前生成的输出:

    for the_file in list_dir:
        if currency in the_file:
            temp_df = pd.read_hdf(folder + the_file)
            ...

我也使用一些select命令从数据文件中获取特定的列:

    with pd.HDFStore('data/observer/' + self.currency + '_' + timestamp) as hdf:
        df = hdf.select(key='observers', columns=[x, y])

错误回溯:

File ".../data_processing/observer_data.py", line 52, in save_obs_to_pandas
hdf.append(key='observers', value=df, format='table', data_columns=True)
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 963, in append
**kwargs)
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 1341, in _write_to_group
s.write(obj=value, append=append, complib=complib, **kwargs)
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 3930, in write
self.set_info()
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 3163, in set_info
self.attrs.info = self.info
File ".../venv/lib/python3.5/site-packages/tables/attributeset.py", line 464, in __setattr__
nodefile._check_writable()
File ".../venv/lib/python3.5/site-packages/tables/file.py", line 2119, in _check_writable
raise FileModeError("the file is not writable")
tables.exceptions.FileModeError: the file is not writable
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File ".../general_manager.py", line 144, in <module>
gm.run()
File ".../general_manager.py", line 114, in run
list_of_observer_managers = self.load_all_observer_managers()
File ".../general_manager.py", line 64, in load_all_observer_managers
observer = currency_pool.map(self.load_observer_manager, list_of_currencies)
File "/usr/lib/python3.5/multiprocessing/pool.py", line 260, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
tables.exceptions.FileModeError: the file is not writable
python python-3.x pandas hdf5
1个回答
1
投票

手头的问题是我搞砸了OS文件权限。我试图读取的文件属于root(因为我运行了用root生成这些文件的代码),我试图用user帐户访问它们。

我正在运行debian,以下命令(如root)解决了我的问题:

chown -R user.user folder

此命令以递归方式将该文件夹中所有文件的权限更改为user.user

© www.soinside.com 2019 - 2024. All rights reserved.