[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to append horizontally the data array from the FOR loop?

On 10May2019 08:08, Madhavan Bomidi <blmadhavan at> wrote:
>I have to append requisite data matrix from multiple files into a 
>single variable using FOR loop.
>outData = [];
>for file in fileList:
>   ....
>   allData = ....                # an array of nrows and ncols.
>   outData = [outData; allData]  # in MATLAB
>While the ncols are constant, the nrows will vary. My intention is to 
>append the allData (with variable rows) from each file into the 
>1. Can anyone suggest how I can do this with an example?

See the .append and .extend methods for lists in the python 
documentation. (It is often easiest to go to the index and look up the 
method if the documentation section is not obvious).

Which method you want depends on what "allData" really is. I would guess 
.append, that is the common case.

>2. How can I save this outData (data matrix) with delimiter (comma) and precision of '%8.5f' or any other into a .txt or .csv file or any other type?

As Rhodri has suggested, see the "csv" module in the documentation. Open 
a csv writer, and assemble rows and write them out in a loop.

>3. How can I append the header on the top of this output file?

You don't. You write the header first, as the first row.

Cameron Simpson <cs at>