【问题标题】:Conversion .dat to .mat using Python使用 Python 将 .dat 转换为 .mat
【发布时间】:2019-10-20 07:33:01
【问题描述】:

我有一个目录,其中有扩展名为 .dat 的文件。 如何快速将此目录中的所有文件转换为 .mat 扩展。

【问题讨论】:

  • 转换或重命名?
  • @BogdanDoicin 转换
  • 您确定 .dat 文件的格式可以被 Matlab 读取吗?因为重命名很容易
  • @SoH:如果我们不知道文件是什么格式,我们如何帮助您转换文件?没有标准的.dat 格式。我们不知道里面有什么。
  • @gnovice 如何查看其中存储的内容?

标签: python matlab file-conversion


【解决方案1】:

.mat 文件是包含可由 Matlab 读取的数据的文件。如果.dat 文件具有Matlab 也可以读取的格式,那么您的问题是一个简单的文件重命名问题。更准确地说,将文件夹中所有文件的扩展名从dat更改为mat

代码将是这样的:

# Python3 code to rename multiple 
# files in a directory or folder 

# importing os module 
import os 

# Function to rename multiple files 
def main(): 
    i = 0

    for filename in os.listdir("xyz"): #xyz=the folder which has your files
        dst ="s0" + str(i) + "lre.mat" #I suppose the numbering of the files begins with 0. This is the name pattern I detected from your screenshot
        src ='xyz'+ filename 
        dst ='xyz'+ dst 

        # rename() function will 
        # rename all the files 
        os.rename(src, dst) 
        i += 1

# Driver Code 
if __name__ == '__main__': 

    # Calling main() function 
    main() 

【讨论】:

    猜你喜欢
    • 2016-08-19
    • 2013-10-05
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2014-05-05
    • 2015-01-05
    相关资源
    最近更新 更多