python 3.x 環境下,使用h5py加載HDF5文件,查看keys,如下:
1
2
3
|
>>> import h5py >>> f = h5py. File ( "a.h5" , 'r' ) >>> f.keys() |
結果看不到keys:
1
|
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) 采用如下代碼:
1
|
>>> [key for key in f.keys()] |
參考資料:
https://stackoverflow.com/questions/31037088/discovering-keys-using-h5py-in-python3
以上這篇解決Python 使用h5py加載文件,看不到keys()的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/MAOJG/article/details/79019017