在python3中使用h5py发现密钥 [英] Discovering keys using h5py in python3

查看:139
本文介绍了在python3中使用h5py发现密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python2.7中,我可以分析hdf5个文件密钥的使用情况

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

$ python
>>> import h5py
>>> f = h5py.File('example.h5', 'r')
>>> f.keys()
[u'some_key']

但是,在python3.4中,我得到了一些不同的东西:

However, in python3.4, I get something different:

$ python3 -q
>>> import h5py
>>> f = h5py.File('example.h5', 'r')
>>> f.keys()
KeysViewWithLock(<HDF5 file "example.h5" (mode r)>)

什么是KeysViewWithLock,如何在Python3中检查我的HDF5密钥?

What is KeysViewWithLock, and how can I examine my HDF5 keys in Python3?

推荐答案

从h5py的网站(

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

在Python 3中使用h5py时,keys(),values()和items() 方法将返回类似视图的对象,而不是列表.这些对象 支持集装箱船测试和迭代,但不能像 列表.

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())

不幸的是,我在iPython中运行东西,它使用命令'l'.这意味着这种方法行不通.

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()]

然后您得到:

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

这篇关于在python3中使用h5py发现密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆