I am having difficulty loading in 'str' variables 'Et' (Endtime) and 'St' (Starttime) from a MATLAB .mat file into Python.
I want identical output as in MATLAB. Instead I have had issues trying to solve this. See below for Python code and output.
# Import numpy and h5py to load in .mat files
import numpy as np
import h5py
# Load in Matlab ('-v7.3') data
fname = 'directory/file.mat'
f = h5py.File(fname,'r')
# create dictionary for data
data= {"average":np.array(f.get('average')),"median":np.array(f.get('median')), \
"stdev":np.array(f.get('stdev')),"P10":np.array(f.get('p10')), \
"P90":np.array(f.get('p90')),"St":np.str(f.get('stime')), \
"Et":np.str(f.get('etime'))}
# All other variables are arrays
print(data["Et"])
output:
<HDF5 dataset "etime": shape (1, 6), type "<u4">
I want to have a string in python equal to the string in MATLAB. In other words, I want print(data["Et"]) = '01011212000000' which is the date and time.
How can I solve this?

f['average']has 2 datasets, 'type' and 'value'. It's a good idea to read both separately. For a stringtypeisb'sq_string', andvalueis a (n,1) array of 'int8' dtype. That could, I think be cast to a Pythonbytestring. There have been a few of SO questions that explore loadinghdf5mat files, though I don't recall if any looked at strings.f.get('etime')? Is it a group or a dataset? If a group, does it have any keys?np.array(f.get('etime')). Load it as an array; we might be able to 'decode' it after, as I do in myIn[138].np.array(f.get('etime'), dtype='<u4'). Or usebytesas suggested by `@machnic.