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

yipeiwu_com7年前Python基础

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

以上这篇解决Python 使用h5py加载文件,看不到keys()的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

简单介绍Python中利用生成器实现的并发编程

我们都知道并发(不是并行)编程目前有四种方式,多进程,多线程,异步,和协程。 多进程编程在python中有类似C的os.fork,当然还有更高层封装的multiprocessing标准库...

我就是这样学习Python中的列表

1. 列表 对于python列表的理解可以和C语言里面的数组进行比较性的记忆与对照,它们比较相似,对于python里面列表的定义可以直接用方括号里加所包含对象的方法,并且python的...

用python实现对比两张图片的不同

用python实现对比两张图片的不同

from PIL import Image from PIL import ImageChops def compare_images(path_one, path_two,...

Python中的类与类型示例详解

Python中的类与类型示例详解

1.经典类与新式类 在了解Python的类与类型前,需要对Python的经典类(classic classes)与新式类(new-style classes)有个简单的概念。 在Pyth...

pytorch对可变长度序列的处理方法详解

pytorch对可变长度序列的处理方法详解

主要是用函数torch.nn.utils.rnn.PackedSequence()和torch.nn.utils.rnn.pack_padded_sequence()以及torch.nn...