import h5py
import csv
data = h5py.File('/home/disk1/xs/nyu_depth_v2_labeled.mat')
print(data.keys())
names = data['names']
print(names) # <HDF5 dataset "names": shape (1, 894), type "|O">
print(names.shape) # (1, 894)
print(names.dtype) # object
print(names[0][0]) # <HDF5 object reference>
print(data[names[0][0]]) # <HDF5 dataset "FFb": shape (4, 1), type "<u2">
注: type "<u2"> 是 uint16 的缩写,使用 chr
函数将其转成对应字符
转成 string 并存入 csv 文件
import h5py
import csv
data = h5py.File('/home/disk1/xs/nyu_depth_v2_labeled.mat')
names = data['names']
with open('name.csv', 'w') as f:
writer = csv.writer(f)
for i in range(names.shape[1]):
# name = ''.join([chr(v[0]) for v in data[names[0][i]].value])
# .value 这种方法已经废弃了
name = ''.join([chr(v[0]) for v in data[(names[0][i])]])
writer.writerow([i, name])
网友评论