编程语言
首页 > 编程语言> > 与Matlab textscan等价的Python

与Matlab textscan等价的Python

作者:互联网

https://www.cnpython.com/qa/83484

numpy.loadtxt

numpy.genfromtxt

将MATLAB的textscan转换为Python+NumPy的^{}的示例:

让我们的数据文件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

Matlab代码:

fileID = fopen('results.csv');
d = textscan(fileID,'%f %s %d %d %d %d', 'delimiter',',');
fclose(fileID);

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()

标签:10,Python,i4,sym2,textscan,Matlab,csv
来源: https://www.cnblogs.com/focus-z/p/15070505.html