python程序, 实现matlab的.mat格式转化为dict / json .
- 第一个参数mat_path代表需要转化的mat路径;
- 第二个参数, 如果需要把字典序列化成json, 添加这一参数, 代表json存放位置;
- 返回值: 转化好的字典
import os
import json
import scipy.io as spio
import pandas as pd
def loadmat(filename):
'''
this function should be called instead of direct spio.loadmat
as it cures the problem of not properly recovering python dictionaries
from mat files. It calls the function check keys to cure all entries
which are still mat-objects
'''
data = spio.loadmat(filename, struct_as_record=False, squeeze_me=True)
return _check_keys(data)
def _check_keys(dict):
'''
checks if entries in dictionary are mat-objects. If yes
todict is called to change them to nested dictionaries
'''
for key in dict:
if isinstance(d