如何将具有 1 列的数组拆分为多列(numpy 数组)

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

我目前有一个 .txt 文件已作为列表加载到 python 中,然后作为单列放入 np 数组,其中 n 行取决于文件大小。该文件的顶部和底部的行已被修剪以进行清理。相关代码如下:

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

然后数组打印如下:比我要处理的 1990 年代的 .txt 文件干净得多...

我的问题是,在上面列出的最后几行代码中将打开的文件定义为 numpy 数组后,我无法将这一列拆分为多列。我正在尝试为由 space、" " 分隔的所有内容生成一个列,但在这样做时遇到了问题。可能还需要注意的是,当我使用 numpy.shape(list_array) 时,它 returns 什么都没有,就好像数组没有被正确解释一样。总体目标是将这个 1col x n-rows 数组变成 7col x n-rows,每个值被 " " 分割在文本文件中。如果有人可以帮助我解决这个问题,我将不胜感激。

全部修复。我确实错误地拆分了 txt 文件,使用 line.split() 解决了这个问题。 pandas 也是必要的,因为这不是 np.js 的最佳应用程序。感谢大家的反馈。