I'm relatively new to Python and wondering what is the best way to import data from multiple files into one array. I have quite a few text files containing 50 rows from two data columns (with column separators), such as:
Length=10.txt: 1, 10 2, 30 3, 50
-
Length=20.txt 1, 50.7 2, 90.9 3, 10.3
Let's say I have 10 text files to import and import into a variable called data.
I would like to create one three-dimensional array containing all the data. This way, I can easily create and process data by referencing data[:,:,n] , where n refers to the index of the text file.
I think I would do this to have an array of shapes (50, 2, 10), but I don't know how best to use python to create it. I thought about using a loop to import each text file as a 2D array, and then stacked it to create a 2D array, although I could not find the appropriate commands for this (I looked at vstack and column_stack in numpy, but these don't seem to add additional measurement).
So far I have written an import code:
file_list = glob.glob(source_dir + '/*.TXT')
But the problem with this code is that I can only process data in a for loop.
What I really want is an array of all the data imported from text files.
Any help would be greatly appreciated!
Ianroberts
source share