python读取hdf5文件,如何在Python中读取HDF5文件

I am trying to read data from hdf5 file in Python. I can read the hdf5 file using h5py, but I cannot figure out how to access data within the file.

My code

import h5py

import numpy as np

f1 = h5py.File(file_name,'r+')

This works and the file is read. But how can I access data inside the file object f1?

解决方案

Read HDF5

import h5py

filename = "file.hdf5"

with h5py.File(filename, "r") as f:

# List all groups

print("Keys: %s" % f.keys())

a_group_key = list(f.keys())[0]

# Get the data

data = list(f[a_group_key])

Write HDF5

import h5py

# Create random data

import numpy as np

data_matrix = np.random.uniform(-1, 1, size=(10, 3))

# Write data to HDF5

with h5py.File("file.hdf5", "w") as data_file:

data_file.create_dataset("group_name", data=data_matrix)

See h5py docs for more information.

Alternatives

JSON: Nice for writing human-readable data; VERY commonly used (read & write)

CSV: Super simple format (read & write)

pickle: A Python serialization format (read & write)

XML: exists too *sigh* (read & write)

For your application, the following might be important:

Support by other programming languages

Reading / writing performance

Compactness (file size)

In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python

你可能感兴趣的