读取HDF5
import h5pyfilename = "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])写HDF5
import h5py# Create random dataimport numpy as npdata_matrix = np.random.uniform(-1, 1, size=(10, 3))# Write data to HDF5with h5py.File("file.hdf5", "w") as data_file: data_file.create_dataset("group_name", data=data_matrix)有关更多信息,请参见h5py docs。
备择方案
- JSON:非常适合编写人类可读的数据;非常常用(读和写)
- CSV:超简单格式(读写)
- pickle:一种Python序列化格式(读写)
- MessagePack(Python软件包):更紧凑的表示形式(读和写)
- HDF5(Python软件包):非常适合矩阵(读写)
- XML:存在太多叹息(读与写)
对于您的应用程序,以下内容可能很重要:
- 其他编程语言的支持
- 阅读/写作表现
- 紧凑度(文件大小)
另请参阅:数据序列化格式的比较
如果您想寻找一种制作配置文件的方法,则可能需要阅读我的短文《Python中的配置文件》。



