初学Python,遇到需要将mat文件转为csv文件,看了很多博客,最后找到了解决办法,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#方法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 )
|
1
2
3
4
5
6
7
8
9
|
#方法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 )
|
以上这篇Python读取mat文件,并转为csv文件的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zebralxr/article/details/78254192