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

yipeiwu_com6年前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 range、enumerate和zip函数用法详解

前言 range函数可创建一个整数列表。 如果需要知道当前元素在列表中的索引,推荐用enumerate代替range。 zip函数用于同时遍历多个迭代器。 一、range 函数 ra...

基于Python __dict__与dir()的区别详解

Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案。 __dict__与dir()的区别: dir()是一个函数,返回的是lis...

python+selenium实现简历自动刷新的示例代码

python+selenium实现简历自动刷新的示例代码

本文用到的文件的下载地址 百度网盘链接: https://pan.baidu.com/s/1tmpdEfAZKff5TOMAitUXqQ 提取码: e6at 1 安装Python 和 s...

Pytorch之finetune使用详解

finetune分为全局finetune和局部finetune。首先介绍一下局部finetune步骤: 1.固定参数 for name, child in model.named...

python绘制简单折线图代码示例

python绘制简单折线图代码示例

1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] p...