解決Python 使用h5py加載文件,看不到keys()的問題
python 3.x 環(huán)境下,使用h5py加載HDF5文件,查看keys,如下:
>>> import h5py >>> f = h5py.File("a.h5",'r') >>> f.keys()
結(jié)果看不到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 環(huán)境進(jìn)行相同操作。
2) 采用如下代碼:
>>> [key for key in f.keys()]
參考資料:
https://stackoverflow.com/questions/31037088/discovering-keys-using-h5py-in-python3
以上這篇解決Python 使用h5py加載文件,看不到keys()的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python使用numpy實現(xiàn)直方圖反向投影示例
今天小編就為大家分享一篇python使用numpy實現(xiàn)直方圖反向投影示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python Spyder 調(diào)出縮進(jìn)對齊線的操作
這篇文章主要介紹了Python Spyder 調(diào)出縮進(jìn)對齊線的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02淺談python socket函數(shù)中,send與sendall的區(qū)別與使用方法
下面小編就為大家?guī)硪黄獪\談python socket函數(shù)中,send與sendall的區(qū)別與使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05