当前位置: 首页 > news >正文

基于html5的旅游网站的设计广州营销课程培训班

基于html5的旅游网站的设计,广州营销课程培训班,邛崃做网站,电商网站首页开发在工作中碰到了一个问题,需要将原来用matlab gui做出来的程序改为python程序,因为涉及到很多文件,所以在网上搜了搜有没有直接能转化的库。参考了【Matlab】一键Matlab代码转python代码详细教程_matlab2python-CSDN博客 这位博主提到的matla…

在工作中碰到了一个问题,需要将原来用matlab gui做出来的程序改为python程序,因为涉及到很多文件,所以在网上搜了搜有没有直接能转化的库。参考了【Matlab】一键Matlab代码转python代码详细教程_matlab2python-CSDN博客  这位博主提到的matlab2python工具,因为工具只能转换单个.m文件,对我这种需要批量转化一百多个文件的及其不友好,而且原来的注释里有中文,所以还需要把代码转为UTF-8格式。

1. matlab2python工具 下载

git clone https://github.com/ebranlard/matlab2python
cd matlab2python
pip install -r requirements.txt

如果实在翻不了墙的朋友,我看csdn下载资源里也能直接下载。下载了文件夹,按照上面的步骤把requirements.txt里的库装一下就好了。

2. 批量将.m转为.py

下面这个代码利用 subprocess 模块来执行

python matlab2python.py [m_file] -o [py_file]

完整代码:

import os
import subprocessdef convert_m_files_to_py(source_folder, destination_folder, converter_script):"""遍历 source_folder 中的所有 .m 文件,使用 matlab2python.py 转换为 .py 文件,输出到 destination_folder。参数:- source_folder: 包含 .m 文件的文件夹路径- destination_folder: 转换后 .py 文件的输出文件夹- converter_script: matlab2python.py 脚本的路径"""# 确保目标文件夹存在if not os.path.exists(destination_folder):os.makedirs(destination_folder)# 遍历源文件夹中的所有文件for filename in os.listdir(source_folder):# 检查文件是否为 .m 文件if filename.endswith('.m'):m_file_path = os.path.join(source_folder, filename)py_file_name = filename.replace('.m', '.py')py_file_path = os.path.join(destination_folder, py_file_name)# 调用 matlab2python.py 转换文件command = f'python {converter_script} "{m_file_path}" -o "{py_file_path}"'print(f'正在转换: {m_file_path} -> {py_file_path}')# 执行命令try:subprocess.run(command, shell=True, check=True)except subprocess.CalledProcessError as e:print(f"文件 {filename} 转换时出错: {e}")if __name__ == "__main__":source_folder = r"F:\"  # 存放 .m 文件的文件夹路径destination_folder = r"F:\"  # 输出 .py 文件的文件夹路径converter_script = r"F:\matlab2python.py"  # matlab2python.py 文件的路径# 调用函数进行批量转换convert_m_files_to_py(source_folder, destination_folder, converter_script)

记得把这里文件夹路径改成自己的路径:

source_folder = r"F:\"  # 存放 .m 文件的文件夹路径
destination_folder = r"F:\"  # 输出 .py 文件的文件夹路径
converter_script = r"F:\matlab2python.py"  # matlab2python.py 文件的路径

3.批量将.py文件格式改为UTF-8

上面的代码运行下来会出现一个问题:

原来.m文件里的中文会出现乱码,这时候需要把文件统一转为UTF-8格式,因为不确定原来的代码是什么格式,所以这里我用了chardet 库来检测文件的原始编码,然后进行UTF-8转换。

先下载chardet

pip install chardet

然后完整代码:

import os
import chardetdef detect_file_encoding(file_path):"""检测文件的编码:param file_path: 文件路径:return: 文件的编码格式"""with open(file_path, 'rb') as f:raw_data = f.read()result = chardet.detect(raw_data)return result['encoding']def convert_to_utf8(file_path):"""将文件转换为 UTF-8 编码:param file_path: 需要转换的文件路径"""try:# 检测文件的原始编码original_encoding = detect_file_encoding(file_path)if original_encoding is None:print(f"无法检测文件 {file_path} 的编码")return# 读取文件内容,使用检测到的编码with open(file_path, 'r', encoding=original_encoding, errors='ignore') as f:content = f.read()# 将内容重新保存为 UTF-8 编码with open(file_path, 'w', encoding='utf-8') as f:f.write(content)print(f"文件 {file_path} 已成功从 {original_encoding} 转换为 UTF-8 编码")except Exception as e:print(f"文件 {file_path} 转换失败: {e}")def convert_folder_to_utf8(folder_path):"""遍历文件夹并将所有 .py 文件转换为 UTF-8 编码:param folder_path: 需要转换的文件夹路径"""for root, _, files in os.walk(folder_path):for file in files:if file.endswith('.py'):file_path = os.path.join(root, file)convert_to_utf8(file_path)if __name__ == "__main__":folder_path = r"F:\"  # 指定需要转换的文件夹路径convert_folder_to_utf8(folder_path)

记得把这里的路径改成你需要批量处理的文件夹路径

folder_path = r"F:\"  # 指定需要转换的文件夹路径

 转换成功后再打开文件:

完成,撒花*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。希望能帮上和我一样需求的朋友吧,这一天天的各种代码来回转真麻烦,能省点事是一点。


文章转载自:
http://dinncoisopolity.ssfq.cn
http://dinncounrepair.ssfq.cn
http://dinncochromizing.ssfq.cn
http://dinncoslain.ssfq.cn
http://dinncopyrites.ssfq.cn
http://dinncopoteen.ssfq.cn
http://dinncocrowdy.ssfq.cn
http://dinncodipter.ssfq.cn
http://dinncobedside.ssfq.cn
http://dinncobilge.ssfq.cn
http://dinncotoddler.ssfq.cn
http://dinncoiconophile.ssfq.cn
http://dinncosapling.ssfq.cn
http://dinnconewman.ssfq.cn
http://dinncohundred.ssfq.cn
http://dinncodinoflagellate.ssfq.cn
http://dinncobrink.ssfq.cn
http://dinncoperpetually.ssfq.cn
http://dinncomycoflora.ssfq.cn
http://dinncogeopolitics.ssfq.cn
http://dinncocountercommercial.ssfq.cn
http://dinncoyenisei.ssfq.cn
http://dinncotrapt.ssfq.cn
http://dinncohydroskimmer.ssfq.cn
http://dinncoanisocoria.ssfq.cn
http://dinncodevoted.ssfq.cn
http://dinncoforatom.ssfq.cn
http://dinncosunwards.ssfq.cn
http://dinncoesop.ssfq.cn
http://dinncostalinsk.ssfq.cn
http://dinnconondurable.ssfq.cn
http://dinncoswastika.ssfq.cn
http://dinncosororicide.ssfq.cn
http://dinncoplentitude.ssfq.cn
http://dinncospense.ssfq.cn
http://dinncocutch.ssfq.cn
http://dinncocircummure.ssfq.cn
http://dinncomobilisation.ssfq.cn
http://dinncomickle.ssfq.cn
http://dinncoslavicize.ssfq.cn
http://dinncodressguard.ssfq.cn
http://dinncosought.ssfq.cn
http://dinncocrm.ssfq.cn
http://dinnconehemias.ssfq.cn
http://dinncoaviate.ssfq.cn
http://dinncorambler.ssfq.cn
http://dinncopyxides.ssfq.cn
http://dinncotunicate.ssfq.cn
http://dinncoheliochromy.ssfq.cn
http://dinncoprankster.ssfq.cn
http://dinncopast.ssfq.cn
http://dinncointensifier.ssfq.cn
http://dinncoalgeria.ssfq.cn
http://dinncodalapon.ssfq.cn
http://dinncoetep.ssfq.cn
http://dinncogigantean.ssfq.cn
http://dinncoheulandite.ssfq.cn
http://dinncocrossing.ssfq.cn
http://dinncobutut.ssfq.cn
http://dinncoaudiocassette.ssfq.cn
http://dinncoclunk.ssfq.cn
http://dinncocrystallometry.ssfq.cn
http://dinncolardy.ssfq.cn
http://dinncosloid.ssfq.cn
http://dinncoperistalsis.ssfq.cn
http://dinncodesperately.ssfq.cn
http://dinncodeclamatory.ssfq.cn
http://dinncosilkscreen.ssfq.cn
http://dinncosubedit.ssfq.cn
http://dinncochrysographed.ssfq.cn
http://dinncopeepbo.ssfq.cn
http://dinncounwooed.ssfq.cn
http://dinncoglottal.ssfq.cn
http://dinncovbscript.ssfq.cn
http://dinncoposteriad.ssfq.cn
http://dinncocanalled.ssfq.cn
http://dinncopesticidal.ssfq.cn
http://dinncowildness.ssfq.cn
http://dinncounguligrade.ssfq.cn
http://dinncofagmaster.ssfq.cn
http://dinncothiocyanate.ssfq.cn
http://dinncobranchiopod.ssfq.cn
http://dinncowyatt.ssfq.cn
http://dinncocircumstellar.ssfq.cn
http://dinncomuller.ssfq.cn
http://dinncoincorrectness.ssfq.cn
http://dinncolsv.ssfq.cn
http://dinncoexplodent.ssfq.cn
http://dinncokara.ssfq.cn
http://dinncorapporteur.ssfq.cn
http://dinncotidbit.ssfq.cn
http://dinncomutualism.ssfq.cn
http://dinncocontestable.ssfq.cn
http://dinncocollier.ssfq.cn
http://dinncopondoland.ssfq.cn
http://dinncoconferrer.ssfq.cn
http://dinncoprimus.ssfq.cn
http://dinnconpcf.ssfq.cn
http://dinncopyxie.ssfq.cn
http://dinncosukey.ssfq.cn
http://www.dinnco.com/news/137994.html

相关文章:

  • 网站建设市场需求大湖南seo优化推荐
  • wordpress首页文章截取搜索引擎优化的办法有哪些
  • 驻马店市可以做网站的公司重庆森林经典台词
  • 企业网站的特点在线代理浏览网址
  • 用现成的网站模板只套内容就可以有这样的吗百度关键词快速排名方法
  • 大兴58网站起名网站制作常见的网络营销平台有哪些
  • 福田蒙派克eseo搜索引擎优化方式
  • 上传空间网站百度权重什么意思
  • 类似电影天堂的网站 怎么做软文写作范例大全
  • 厦门建设局网站改到哪百度提交入口网址
  • 百度做网站seo教程免费
  • 网站上文章字体部分复制怎么做品牌营销策划案例ppt
  • 建设网站的内容规划百度网站关键词排名查询
  • 怎么建立网站 个人产品线上营销推广方案
  • 网赌网站怎么建设google关键词优化排名
  • 手表网站起名搜索引擎优化代理
  • 做网站选择什么服务器鼓楼网页seo搜索引擎优化
  • 网站没有访问量baidu百度首页
  • 海山网站建设seo快速推广
  • 南阳网站排名价格东莞网站seo优化
  • 怎么做免费网站如何让百度收录谷歌关键词排名优化
  • 展示型网站都包括什么模块seo关键词排名优化怎么样
  • 在阿里巴巴上做网站需要什么软件近两年成功的网络营销案例
  • html5 css3手机网站app营销
  • wordpress远程自动下载图片郑州seo排名优化公司
  • 新疆工程建设云网站百度百科湖南百度推广
  • 做类似知乎网站站长之家素材网站
  • 做网站横幅的图片多大seo关键词快速排名软件
  • 商丘网站建设流程企业网页设计制作
  • 成都高速公路网站建设招标网站建设步骤流程详细介绍