网络编程
位置:首页>> 网络编程>> Python编程>> 解决Python 使用h5py加载文件,看不到keys()的问题

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

作者:MAOJG  发布时间:2021-10-04 06:19:15 

标签:Python,h5py,keys

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


>>> import h5py
>>> f = h5py.File("a.h5",'r')
>>> f.keys()

结果看不到keys:


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

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

官方说明如下:

When using h5py 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-h5py-in-python3

来源:https://blog.csdn.net/MAOJG/article/details/79019017

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com