如果你正在将Matlab翻译成Python,我会假设你已经在使用NumPy了.
在这种情况下,您可以使用np.loadtxt(如果没有值丢失)或np.genfromtxt(如果缺少值:我不确定Matlab是否textscan处理该值).
如果您需要更多帮助,请向我们提供更多详细信息!
MATLAB转换textscan为Python + NumPy的示例np.loadtxt:
让我们的数据文件results.csv包含:
0.6236,sym2,1,5,10,10
0.6044,sym2,2,5,10,10
0.548,sym2,3,5,10,10
0.6238,sym2,4,5,10,10
0.6411,sym2,5,5,10,10
0.7105,sym2,6,5,10,10
0.6942,sym2,7,5,10,10
0.6625,sym2,8,5,10,10
0.6531,sym2,9,5,10,10
Run Code Online (Sandbox Code Playgroud)
Matlab代码:
fileID = fopen('results.csv');
d = textscan(fileID,'%f %s %d %d %d %d', 'delimiter',',');
fclose(fileID);
Run Code Online (Sandbox Code Playgroud)
Python + NumPy代码:
fd = open('results2.csv','r')
d = np.loadtxt(fd,
delimiter=',',
dtype={'names': ('col1', 'col2', 'col3', 'col4', 'col5', 'col6'),
'formats': ('float', 'S4', 'i4', 'i4', 'i4', 'i4')})
fd.close()
Run Code Online (Sandbox Code Playgroud)
有关类型的更多信息,请参阅数据类型对象(dtype).