Search code examples
arraysnumpysplitmultiple-columnscolumnsorting

How to split array with 1 column into multiple columns (numpy array)


I currently have a .txt file that has been loaded into python as a list, and then placed into a np array as a single column with n number of rows depending on the file size. The file has rows trimmed off the top and bottom to clean it up. Relevant code is shown below:

# Open the .txt file in the user selected folder

txtFile = glob.glob(folderView + "**/*.txt", recursive = True)
print (" The text files in the folder " + userInput + " are: " + str(txtFile))
# The quantitative output epatemp.txt file is always index position 1 in the glob list.
epaTemp = txtFile[1]
print (epaTemp)

###

quantData = open(epaTemp, 'r')
print (quantData)
result = [line.split(',') for line in quantData.readlines()[24:]]
print (result)
n = 4
result = result[:-n or None]
print (result)
print (" Length of List: " + str(len(result)))
print (result[0])

### Loop through all list components and split each
print (" NUMPY STUFF! ")
list_array = np.array(result)
print (list_array)

The array is then printed like this: much cleaner than the 1990's .txt files I am given to process...

Numpy_Array_Print

My issue is that I am unable to split this single column into multiple columns after defining the opened file as a numpy array in the final lines of code listed above. I am trying to produce a single column for everything that is separated by a space, " ", but am having trouble doing so. It might also be important to note that when I use numpy.shape(list_array), it returns nothing as if the array is not being interpreted properly. The overall goal is to turn this 1col x n-rows array into 7col x n-rows for every value split by a " " in the text file. If anyone can help me with this issue I'd really appreciate it.


Solution

  • All fixed. I did split the txt file incorrectly, Using line.split() solved the issue. pandas was also necessary, as this was not the best application for np. Thanks everyone for the feedback.