为了将.mat
格式的文件转换为.xlsx
格式,并保持原有的文件名,你可以使用Python中的scipy.io
库来加载.mat
文件,并使用pandas
库来将数据保存为.xlsx
文件。下面是一个示例代码,它可以递归地遍历指定目录下的所有子目录,找到所有.mat
文件,并将它们转换为.xlsx
文件:
import os
import scipy.io
import pandas as pd
def convert_mat_to_xlsx(root_dir):
for subdir, dirs, files in os.walk(root_dir):
for file in files:
if file.endswith('.mat'):
# 构建完整的文件路径
full_path = os.path.join(subdir, file)
# 加载.mat文件
mat_data = scipy.io.loadmat(full_path)
# 遍历文件中的所有变量
for variable_name, data in mat_data.items():
if '__' not in variable_name: # 过滤掉MATLAB的内部变量
# 将numpy数组转换为DataFrame
df = pd.DataFrame(data)
# 构建输出文件的路径,添加变量名以区分不同变量
output_filename = f"{variable_name}.xlsx"
output_path = os.path.join(subdir, output_filename)
# 保存为.xlsx文件
df.to_excel(output_path, index=False)
print(f"Converted {full_path} ({variable_name}) to {output_path}")
# 请将下面的路径替换为你的实际路径
root_directory = r".\CWRU-原始数据"
convert_mat_to_xlsx(root_directory)
确保在运行此代码前,你的环境中已安装了scipy
和pandas
库。你可以使用以下命令进行安装:
pip install scipy pandas
这段代码会遍历指定的根目录及其所有子目录,查找所有.mat
文件,并将它们转换成.xlsx
文件,存储在相同的位置。需要注意的是,.mat
文件可能包含多种结构的数据,因此在实际使用时,可能需要根据你的数据结构进行一些调整。
凯斯西储大学(CWRU)轴承数据(mat格式)百度云下载链接
链接:https://pan.baidu.com/s/1J2VXxRh1hNsnXR2NlNxkcA
提取码:p1dd