I am having trouble figuring out how to translate the following matlab script to python:
clc
clearvars
fileDir = cd;
outfile = 'OUT.xlsx'; % output file name
fileNames = dir(fullfile(fileDir,'*.CSV'));
fileNames_sorted = natsortfiles({fileNames.name});
M= length (fileNames_sorted);
second_col= [];
for f = 1:M
% option # 1 for numeric data only using importdata
raw = importdata( fullfile(fileDir, fileNames_sorted{f}));
second_col= [second_col raw(:,2)]; % extract the second column
end
% write all second columns lines into a matrix and store it in excel file
writematrix(second_col,fullfile(cd,outfile));**
I'm not sure how to translate 'fullfile' and 'natsortfiles' to python functions? This program copies the entire columns from the second row of every excel file in a folder and creates an output file that copies all of the entries into one matrix. Thanks
fullfileyou can probably useos.path.join().natsortfilescould probably be substituted withsorted()orlist.sort().np.loadtxtto read each input file,np.hstackto concatenate, and see this answer for saving to .xlsx. For anything else see the numpy docs starting from numpy.org/doc/stable/user/numpy-for-matlab-users.html and if you get stuck, update the question with your Python code and explain what's going wrong.