解决Python 使用h5py加载文件,看不到keys()的问题

2023-04-19,,

python 3.x 环境下,使用h6py加载HDF5文件,查看keys,如下:

>>> import h6py
>>> f = h6py.File("a.h6",'r')
>>> f.keys()

结果看不到keys:

KeysView(<HDF5 file "a.h6" (mode r)>)

原因主要是 python2.x 和 python3.x对keys方法的返回处理不同。

官方说明如下:

When using h6py from Python 3, the keys(), values() and items() methods will return view-like objects instead of lists. These objects support containership testing and iteration, but can't be sliced like lists.

可见 python2 返回为list,python3 返回为view-like objects,不能直接查看。

解决方法如下:

1) 换成 python2.x 环境进行相同操作。

2) 采用如下代码:

>>> [key for key in f.keys()]

参考资料:

https://stackoverflow.com/questions/31037088/discovering-keys-using-h6py-in-python3

以上这篇解决Python 使用h6py加载文件,看不到keys()的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持本站。

《解决Python 使用h5py加载文件,看不到keys()的问题.doc》

下载本文的Word格式文档,以方便收藏与打印。