如何使用 python 将复杂的 .mat 文件转换为 csv
How can I convert complex .mat file to csv using python
我正在尝试使用 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')
我正在尝试从这个 link 转换 .mat 文件:https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/imdb_meta.tar
请帮我想出一些可行的解决方案!
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.savetxt.html
Save an array to a text file.
不幸的是,您只能在单个文件中存储单个数字 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)"), and store that as a .csv
using How do I convert this list of dictionaries to a csv file? [Python]
中所述
我创建了一个名为 matgrab 的包,可用于将任何 matlab 数据文件转换为 Dataframe。你可以这样称呼它:
import matgrab
matgrab.mat2df(file.mat).to_csv(file.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')
我正在尝试从这个 link 转换 .mat 文件:https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/imdb_meta.tar
请帮我想出一些可行的解决方案!
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.savetxt.html
Save an array to a text file.
不幸的是,您只能在单个文件中存储单个数字 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)"), and store that as a .csv
using How do I convert this list of dictionaries to a csv file? [Python]
我创建了一个名为 matgrab 的包,可用于将任何 matlab 数据文件转换为 Dataframe。你可以这样称呼它:
import matgrab
matgrab.mat2df(file.mat).to_csv(file.csv)