MATLAB打开.m文件注释乱码解决办法——Python版

针对该问题,采用python先按照原编码读取,再用目标编码重新写的方式解决。
可对单个m文件操作,也可对该文件夹及其子文件下的所有m文件进行转换。代码如下。

"""
2024-11-14,20:58
by ltf
"""
import os
import chardet
import shutil
from pathlib import Path


# 第一步,将整个文件夹复制到一个新的文件夹
# 第二步,在新文件夹下,转换M文件编码

def copy_folder(source_folder_path, target_folder_path):
    """复制整个文件夹及其内容"""
    try:
        shutil.copytree(source_folder_path, target_folder_path, dirs_exist_ok=True)

        print(f"文件夹成功复制到: {target_folder_path}")
    except Exception as e:
        print(f"复制失败: {e}")


def detect_encoding(file_path):
    """检测文件编码"""
    with open(file_path, 'rb') as f:
        raw_data = f.read(2000)  # 读取文件的一部分来检测编码
    result = chardet.detect(raw_data)
    return result['encoding']


def get_m_files(source_folder):
    """获取文件夹及子文件夹下所有 .m 文件,返回二维列表"""
    all_m_files = []

    # 使用 os.walk 遍历目录及其子目录
    for root, dirs, files in os.walk(source_folder):
        # 仅获取 .m 文件
        m_files_in_folder = [Path(root) / filename for filename in files if filename.endswith('.m')]

        # 如果当前文件夹下有 .m 文件,添加到 all_m_files 列表中
        if m_files_in_folder:
            all_m_files.append(m_files_in_folder)
    return all_m_files


def convert_file_encoding(source_file_path, target_file_path, target_encoding):
    """将一个 .m 文件转换为目标编码并保存"""
    try:
        # 检测源文件编码
        source_encoding = detect_encoding(source_file_path)
        print(f" {Path(source_file_path).name}", end=" ")
        # 如果文件不是目标编码,根据检测结果读取并转为目标编码
        if source_encoding.lower() != target_encoding:
            print(f" 使用的编码是 {source_encoding},", end=" ")

            # 使用检测到的编码读取文件内容
            with open(source_file_path, 'r', encoding=source_encoding) as source_file:
                content = source_file.read()

            # 将内容以目标编码写入目标文件
            with open(target_file_path, 'w', encoding=target_encoding) as target_file:
                target_file.write(content)
        print(f"成功转换!!!")
    except Exception as e:
        print(f"转换失败: {Path(source_file_path).name}, 错误: {e}")


def convert_all_files(source_folder, target_encoding):
    """ 循环转换文件夹中的所有 .m 文件"""
    # 获取所有 .m 文件
    mfilefoldes = get_m_files(source_folder)
    for mfiles in mfilefoldes:
        print('=====================================')
        print("正在转换" + os.path.dirname(mfiles[0]))
        for mfile in mfiles:
            convert_file_encoding(mfile, mfile, target_encoding)
        print('=====================================')


def main():
    source_folder = r'E:\Code\matlab\myfold'  # 替换为要转换的文件夹路径
    target_folder = source_folder + '_要转换的副本'

    target_encoding = "GBK"  # "utf-8","GBK"
    # 1、复制
    copy_folder(source_folder, target_folder)
    # 2、转换
    convert_all_files(target_folder, target_encoding)


if __name__ == '__main__':
    main()

参考文献:

  1. MATLAB打开.m文件乱码解决办法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值