首页 > 解决方案 > 使用python从hdf5文件中检索属性

问题描述

我想使用 Python 3 从 hdf5 文件中的属性中检索字符串。

除了引号之外,它前面还有一个“b”。如何删除 b 和引号

import h5py
f = h5py.File('.../HS-L1C-FA-166db-00.hdf5', 'r')
aq_time=f['LEVEL1C']['VNIR0'].attrs['TIMESTAMP']


>>> aq_time
b'2018-11-01T11:43:55Z'
>>> aq_time[2:]
b'18-11-01T11:43:55Z'

标签: attributeshdf5h5py

解决方案


HDF5中的所有字符串都是编码文本,所以需要解码。
很容易添加:

import h5py
f = h5py.File('.../HS-L1C-FA-166db-00.hdf5', 'r')
aq_time=f['LEVEL1C']['VNIR0'].attrs['TIMESTAMP'].decode('utf-8')

有关 h5py 文档中 HDF5 字符串的更多信息:http:
//docs.h5py.org/en/stable/strings.html


推荐阅读