如何将空格分隔的数据读入numpy数组?

我有一个用空格分隔数字的文本文件,例如:

-2 -3 4 -1 -2 1.5 -3

我尝试使用以下代码创建一个带有file元素的numpy数组:

root = tk.Tk()
root.withdraw()
A = np.array([])
arquivox = filedialog.askopenfilename()
# reading datafile
with open(arquivox, "r") as f:
    for termox in f:
        # specifying the separator
        termox = termox.split(' ')
        # converting the elements to float and generating the array
        A = np.append(A, float(termox[0]))
print(A)

但是,我只保存了文件的第一个元素(-2)。我做错了什么?

转载请注明出处:http://www.wlshunda.com/article/20230526/2188264.html