python包安装
~$:pip install h5py
简介
创建和打开h5py文件
f = h5py.File(“pathname”,“w”)
w     create file, truncate if exist
w- or x  create file,fail if exists
r         readonly, file must be exist r+        read/write,file must be exist
a        read/write if exists,create othrewise (default)
删除一个dataset或者group
1  | del group["dataset_name/group_name"]  | 
dataset
什么是dataset
datasets和numpy arrays挺像的
创建一个dataset
1  | f = h5py.File("pathname","w")  | 
第一个参数是dataset的名字, 第二个参数是dataset的shape, dtype参数是dataset中元素的类型。
如何访问一个dataset
1  | dataset = f["dataset_name"] # acess like a python dict  | 
dataset的属性
dataset.name        #输出dataset的名字
dataset.tdype        #输出dataset中elements的type
dataset.shape        #输出dataset的shape
dataset.value
dataset doesn’t hava attrs like keys,values,items,etc…
给h5py dataset复制numpy array
1  | array = np.zero((2,3,4)  | 
group
什么是group
group和字典挺像的
创建一个group
1  | group = f.create_group("group_name") #在f下创建一个group  | 
访问一个group(the same as dataset)
1  | group = f["group_name"] # acess like a python dict  | 
group的属性和方法
group.name        #输出group的名字
以下内容分为python2和python3版本
python 2 版本
group.values()    #输出group的value
group.keys()        #输出gorup的keys
group.items()    #输出group中所有的item,包含group和dataste
python 3 版本
list(group.keys())
list(group.values())
list(group.items())
属性
设置dataset属性
dataset.attrs[“attr_name”]=“attr_value”    #设置attr
print(dataset.attrs[“attr_name”])                #访问attr
设置group属性
group.attrs[“attr_name”]=“attr_value”    #设置attr
print(group.attrs[“attr_name”])                #访问attr
numpy and h5py
1  | f = h5py.File(pathname,"r")  | 
参考文献
1.http://docs.h5py.org/en/latest/index.html
2.https://stackoverflow.com/questions/31037088/discovering-keys-using-h5py-in-python3