简体   繁体   English

读取h5文件

[英]Reading h5 file

I am a new python user and I want to read the data from a h5 file. 我是新的python用户,我想从h5文件中读取数据。 The code that I have used to read the data is given below: 下面是我用来读取数据的代码:

>>> import numpy as np
>>> import h5py
>>> f = h5py.File('file.h5', 'r')
>>> list(f.keys())
[u'data']
>>> dset = f[u'data']
>>> dset.shape
(64, 64, 64)
>>> dset.dtype
dtype(('<f8', (3,)))

Can anyone help me to understand these commands? 谁能帮我理解这些命令? My questions are: 1. What can I understand by the terms [u'data'] and dtype((' 我的问题是:1.我可以通过[u'data']和dtype(('

As @hpaulj mentions, the h5py doc is a good reference. 正如@hpaulj所提到的,h5py文档是一个很好的参考。 You also need to understand basic HDF5 file concepts. 您还需要了解基本的HDF5文件概念。 It's a big topic. 这是一个大话题。 To get started, review the Learning HDF5 pages from The HDF Group. 首先,请查看The HDF Group的Learning HDF5页面。 I know (from personal experience) that you will struggle if you don't understand how to navigate the hierarchy. 我(从个人经验中)知道,如果您不了解如何导航层次结构,您将很挣扎。 Learn the differences between Group and Dataset objects. 了解组对象和数据集对象之间的区别。

Here's an explanation of the output in your OP. 这是您的OP中输出的说明。 Note that these are all h5py functions (not numpy). 请注意,这些都是h5py函数(不是numpy)。

>>> list(f.keys())
[u'data']

The “keys” are the names of group members, and the “values” are the members (Group and Dataset objects). “键”是组成员的名称,“值”是成员(组和数据集对象)。 This is a list of the Node names at the root level of the file. 这是文件根级别的节点名称的列表。 Nodes can be Groups or Datasets. 节点可以是组或数据集。 In your case, you have one dataset named data . 在您的情况下,您有一个名为data集。 (The only group in this HDF5 file is the root group: '/', there are no groups below the root.) (此HDF5文件中的唯一组是根组:“ /”,根下没有任何组。)

The next step accesses the data in the data dataset. 下一步访问数据data集中的数据。

>>> dset = f[u'data']

The shape attribute gives you the dimensions of the dataset: shape属性为您提供数据集的尺寸:

>>> dset.shape
(64, 64, 64)

The dtype attribute gives you the data types of the dataset (just like numpy): dtype属性为您提供数据集的数据类型(就像numpy一样):

>>> dset.dtype
type(('<f8', (3,)))

So, you have an array of floats. 因此,您有一个浮点数数组。 If you want to see the data, you can enter this (I would only do this for small datasets, or slice to print a few rows): 如果要查看数据,可以输入以下内容(我仅对小型数据集执行此操作,或者切片以打印几行):

>>> for row in dset:
        print (row)

From here, what you do with the data is up to you. 从这里开始,您将如何处理数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM