用python讀寫HDF5格式檔案

2021-09-24 05:22:43 字數 1473 閱讀 6947

h5py.file()函式和』w』 選項建立乙個data.h5檔案

create_dataset()函式在hdf5檔案裡面寫入dataset,該檔案裡面有兩個dataset分別是dataset_1和dataset_2。

具體**如下。

import numpy as np

import h5py

data1 = np.random.random(size =

(100

,100))

data2 = np.random.random(size =

(200

,200))

hdffile = h5py.file(

'data.h5'

,'w'

)hdffile.create_dataset(

'dataset_1'

, data=data1)

hdffile.create_dataset(

'dataset_2'

, data=data2)

hdffile.close(

)

h5py.file()函式和』r』 選項讀取乙個data.h5檔案

get()方法得到某個dataset

hdffile = h5py.file(

'data.h5'

,'r'

)dataset1 = hdffile.get(

'dataset_1'

)print

(dataset1.shape)

hdffile.close(

)

group在hdf5中的作用類似於linux的檔案系統層次結構,通過這個層次結構可以合理的將不同的資料組織起來。

我們用create_group()函式建立乙個組。

keys()函式來得到hdf5檔案裡面的group

items()函式可以檢視某個group裡面的dataset。

hdffile = h5py.file(

'data.h5'

,'w'

)group1 = hdffile.create_group(

'group1'

)group1.create_dataset(

'dataset_1'

, data=data1)

group1.create_dataset(

'dataset_2'

, data=data2)

print

(hdffile.keys())

print

(group1.items(

))

HDF5檔案讀寫

做實驗需要跑資料,caffe庫要求資料格式為hdf5.hdf5資料的import與export可以通過matlab簡單完成。hdfview可以簡單檢視hdf5資料。1.h5disp命令可以檢視hdf5資料格式 h5disp test.h5 hdf5 test.h5 group dataset dat...

HDF5檔案讀寫

做實驗需要跑資料,caffe庫要求資料格式為hdf5.hdf5資料的import與export可以通過matlab簡單完成。hdfview可以簡單檢視hdf5資料。1.h5disp命令可以檢視hdf5資料格式 1 h5disp test.h5 2hdf5 test.h5 3 group 4 data...

HDF5 檔案格式系統 Python

1 什麼是hdf5 簡單的來說hdf5是一種新的檔案格式,可以進行大批量檔案的儲存,並且該種儲存格式支援可以通過numpy方便的進行資料的操作 包括切片,keys等操作。具體的介紹參考這篇部落格hdf5介紹。2 python 中 hdf5 模組的使用 貼乙個官方文件 hdf5官方文件 最好的學習方式...