【问题标题】:how to convert many .npz files to .mat files through for loop using python如何使用python通过for循环将许多.npz文件转换为.mat文件
【发布时间】:2020-11-23 00:35:07
【问题描述】:

我需要使用 python 将许多 .npz 文件转换为 .mat 文件。我的文件的排列方式类似于 datafile_1.npz、datafile_2.npz 等我想将它们转换为 datafile_1.mat、datafile_2.mat 文件等。目前我写了一个代码但出现错误 AttributeError: 'str' object has no attribute' items'...为什么这个错误我无法理解。我希望有人能提供帮助。

import numpy as np
import scipy.io
import glob

filpath=glob.glob('./datafile_*.npz')

for file in filpath:
    #data = np.load('file',)
    scipy.io.savemat('filepath'+'.mat', mdict=file)
    print(data)
    
 

【问题讨论】:

  • 以更小的步骤开发您的代码。加载一个 npz 文件,确保你知道如何读取它的所有数组。然后专注于将所有这些写到垫子上。仔细阅读文档。
  • @hpaulj,我在阅读文档后发现这是合适的脚本....您能否建议一些更好的解决方案,以最大限度地减少上述错误
  • 询问错误时,显示产生错误的完整代码以及回溯。首先file 是文件名,所以不能作为savemat 的源字典
  • 第 834 行,在 put_variables 中为名称,在 mdict.items() 中为 var:AttributeError: 'str' object has no attribute 'items'
  • 如何解决这个问题....给任何想法修改代码

标签: python matlab numpy for-loop scipy


【解决方案1】:

使用我从早期 SO 获得的 npz

In [491]: data = np.load('test.npz')                                                                 
In [492]: list(data.keys())                                                                          
Out[492]: ['arr_0', 'arr_1', 'arr_2']
In [493]: data['arr_0']                                                                              
Out[493]: 
array([[1., 1., 1.],
       [1., 1., 1.]])
In [494]: data['arr_1']                                                                              
Out[494]: 
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [495]: data['arr_2']                                                                              
Out[495]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])

data 是一个类似dict 的对象。所以它实际上可以作为mdict 参数传递给savemat(也需要一个dict):

In [496]: io.savemat('data.dat', data)         

测试保存:

In [497]: io.loadmat('data.dat')                                                                     
Out[497]: 
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Sun Aug  2 17:16:18 2020',
 '__version__': '1.0',
 '__globals__': [],
 'arr_0': array([[1., 1., 1.],
        [1., 1., 1.]]),
 'arr_1': array([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]),
 'arr_2': array([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11]])}

或者我们可以制作自己的字典

In [498]: io.savemat('data.dat', mdict={'x':data['arr_0'], 'y':data['arr_1']})                       
In [499]: io.loadmat('data.dat')                                                                     
Out[499]: 
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Sun Aug  2 17:19:06 2020',
 '__version__': '1.0',
 '__globals__': [],
 'x': array([[1., 1., 1.],
        [1., 1., 1.]]),
 'y': array([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])}

关键是要了解np.load 对应npz 返回dict,我们可以从中加载实际数组。这个dict 本身并不是数据。同样,savemat 需要 dict 的数组。由于两者都使用 dicts,我们不必显式加载数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 2018-03-31
    • 1970-01-01
    相关资源
    最近更新 更多