Discovering keys using h5py in python3

后端 未结 1 579
终归单人心
终归单人心 2021-02-12 14:15

In python2.7, I can analyze an hdf5 files keys use

$ python
>>> import h5py
>>> f = h5py.File(\'example.h5\', \'r\')
         


        
1条回答
  •  清歌不尽
    2021-02-12 14:32

    From h5py's website (http://docs.h5py.org/en/latest/high/group.html#dict-interface-and-links):

    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.

    This explains why we can't view them. The simplest answer is to convert them to a list:

    >>> list(for.keys())
    

    Unfortunately, I run things in iPython, and it uses the command 'l'. That means that approach won't work.

    In order to actually view them, we need to take advantage of containership testing and iteration. Containership testing means we'd have to already know the keys, so that's out. Fortunately, it's simple to use iteration:

    >>> [key for key in f.keys()]
    ['mins', 'rects_x', 'rects_y']
    

    I've created a simple function that does this automatically:

    def keys(f):
        return [key for key in f.keys()]
    

    Then you get:

    >>> keys(f)
    ['mins', 'rects_x', 'rects_y']
    

    0 讨论(0)
提交回复
热议问题