2017-10-19 67 views
0

我正試圖使用​​python將.mat文件轉換爲csv。我使用的代碼是我如何使用python將複雜.mat文件轉換爲csv

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=',') 

當過我運行此代碼,我得到的錯誤如下:

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

請幫我解決一些工作問題!

+0

Python允許互動處理。你爲什麼不控制數據的內容? –

+0

對不起,我是新來的python以及matlab,我正在研究一些與使用java的機器學習相關的東西,並且node.js廣告需要一些相關數據並且數據在.mat文件中。所以我只想從這個文件中獲取數據。 – Gaurav

回答

0

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

保存數組到一個文本文件中。

不幸的是,只能在一個文件中存儲單個數字numpy數組。而你.mat文件中包含的結構:

>> 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的字典(如「Complex matlab-like data structure in python (numpy/scipy)」中所述),並存儲爲.csv使用How do I convert this list of dictionaries to a csv file? [Python]

+0

感謝您的回覆,我試圖按照您的步驟,但我是python和matlab的新手,我只知道python基礎知識。我正在尋找從.mat文件獲取數據,因爲我正在爲ML工作,並且需要將內容用於培訓目的。你能給我一個工作的例子,或者一些更詳細的信息與基礎? – Gaurav