Python读取mat文件,并转为csv文件的实例

初学Python,遇到需要将mat文件转为csv文件,看了很多博客,最后找到了解决办法,代码如下:

#方法1
from pandas import Series,DataFrame
import pandas as pd
import numpy as np
import h5py
datapath = 'E:/workspacelxr/contem/data.mat'
file = h5py.File(datapath,'r')
file.keys()
def Print(name):print(name)
data = file['data'][:]
dfdata = pd.DataFrame(data)
datapath1 = 'E:/workspacelxr/contem/data.txt'
dfdata.to_csv(datapath1,index=False)
#方法2
import pandas as pd
import scipy
from scipy import io
features_struct = scipy.io.loadmat('E:/workspacelxr/contem/data.mat')
features = features_struct['data']
dfdata = pd.DataFrame(features)
datapath1 = 'E:/workspacelxr/contem/data.txt'
dfdata.to_csv(datapath1, index=False)

相关推荐