python读mat文件出现错误,Python:使用h5py和NumPy从MATLAB .mat文件读取str时出现问题...

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:

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?

An example of the data in MATLAB:

aJPIl.png

解决方案

If you don't mind the variable type of etime and stime stored in file.mat and you can store them as type char instead of string, you could read them in Python by: bytes(f.get(your_variable).value).decode('utf-8'). In your case:

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": bytes(f.get('stime')[:]).decode('utf-8'),

"Et": bytes(f.get('etime')[:]).decode('utf-8')

}

I'm sure there is also a way of reading string type, but this might be the simplest solution.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值