python读取mat文件,并写入txt文档

mat文件内容

python读取mat文件,并写入txt文档_第1张图片

python读取mat文件

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 17 09:57:22 2019

@author: muli
"""

import scipy.io as scio
 
path = 'label_excel.mat'
data = scio.loadmat(path)
# 查看mat文件的数据格式
#
print(type(data) ) 
                    
#查看字典的键
#dict_keys(['__header__', '__version__', '__globals__', 'bags', 'targets', 'class_name'])
print(data.keys() )             

#dict_keys(['__header__', '__version__', '__globals__', 'X', 'y'])

#选择需要的数据;数组格式
x = data['targets']           
print(x)

参考链接:https://www.cnblogs.com/tongtong123/p/10641599.html

写入txt文档

python读取mat文件,并写入txt文档_第2张图片

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 17 09:57:22 2019

@author: muli
"""

import scipy.io as scio
 
path = 'label_excel.mat'
data = scio.loadmat(path)
# 查看mat文件的数据格式
#
print(type(data) ) 
                    
#查看字典的键
#dict_keys(['__header__', '__version__', '__globals__', 'bags', 'targets', 'class_name'])
print(data.keys() )             

#dict_keys(['__header__', '__version__', '__globals__', 'X', 'y'])

#选择需要的数据;数组格式
x = data['targets']           
#print(x)
print(type(x))

f2=open('nju_labels.txt',mode='a')
for i in range(2000) :
    b=x[:,i]
    for j in range(5):
        if b[j]==1:
            print(j)
            f2.write(str(j)+" ")
    f2.write("\n")        
    print("-----------------------")
#    print(i,b)
f2.close()

你可能感兴趣的:(python)