I have a text file with numbers separated by spaces, such as:
-2 -3 4 -1 -2 1.5 -3
I tried to create an numpy array with the file elements using the following code:
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)
However, I am only saving the first element of the file (-2). What am I doing wrong?
termox[0]... Maybe you need something more likeA = np.array([float(x) for x in termox])float(termox[0])you only take the first element (0).termox, yet you're only float/appending the first item in that list. You know how to iterate over an iterable (you're doing it earlier withfor termox in f).