如何使用python将复杂的.mat文件转换为csv [英] How can I convert complex .mat file to csv using python

查看:1126
本文介绍了如何使用python将复杂的.mat文件转换为csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python将.mat文件转换为csv.我使用的代码是

I am trying to convert a .mat file to csv using python. The code I am using is

import scipy.io
import numpy as np

data = scipy.io.loadmat("wiki.mat")

for i in data:
    if '__' not in i and 'readme' not in i:
        np.savetxt(("file.csv"),data[i],delimiter=',')

每当我运行此代码时,都会出现如下错误:

When ever I run this code, I get error as follows:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    np.savetxt(("file.csv"),data[i],delimiter=',')
  File "/Library/Python/2.7/site-packages/numpy/lib/npyio.py", line 1258, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('[('dob', 'O'), ('photo_taken', 'O'), ('full_path', 'O'), ('gender', 'O'), ('name', 'O'), ('face_location', 'O'), ('face_score', 'O'), ('second_face_score', 'O')]') and format specifier ('%.18e')

我正在尝试从此链接转换.mat文件: https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/imdb_meta.tar

I am trying to convert the .mat file from this link: https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/imdb_meta.tar

请为我提供一些可行的解决方案!

Please help me out with some working solution!

推荐答案

https://docs.scipy.org/doc/numpy-1.13.0/reference/generation/numpy.savetxt.html

将数组保存到文本文件.

Save an array to a text file.

不幸的是,您只能在单个文件中存储单个数字numpy数组.而您的.mat文件包含一个结构:

You can, unfortunately, only store a single numeric numpy array in a single file. Whereas your .mat file contains a structure:

>> fieldnames(imdb)
                        ans = 
                        {
                          [1,1] = dob
                          [2,1] = photo_taken
                          [3,1] = full_path
                          [4,1] = gender
                          [5,1] = name
                          [6,1] = face_location
                          [7,1] = face_score
                          [8,1] = second_face_score                          
                          [9,1] = celeb_names
                          [10,1] = celeb_id
                        }
>> imdb.name(1)        
                        ans = 
                        {
                          [1,1] = Fred Astaire
                        }

将数据转换为numpy字典可能很有意义(如"

It might make sense to convert the data to a numpy dictionary (as described in "Complex matlab-like data structure in python (numpy/scipy)"), and store that as a .csv using How do I convert this list of dictionaries to a csv file? [Python]

这篇关于如何使用python将复杂的.mat文件转换为csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆