I have a text file contains a list (#lines = 1137) of vectors (all are equal size= 1137), am trying to convert this list to 1137*1137 matrix. I created an empty matrix using numpy. But the problem after when I read the file using the following code, the vectors are treated as a sequence of characters, not as a vector or array
myMtrx = np.empty((1137,1137))
f = open("text.txt", "r")
for vector in f:
print len(vector)
arrayF.append(vector.rstrip())
I recognized that by printing our the length of each vector, which is computed based on number of digits not elements in that vector. The vector in the text file looks like
[99.25, 14.74, 26.12, 20.91, 37.14, 79.03, 17.68, 28.4, ...., 0]
so when I print print arrayF[0][0] I receive [, where I need the output to be the 1st element of the 1st vector, which is 99.25.
I tried several ways using numpy, and writing the text file to CSV but nothing works, can you please assist me to solve this issue. You can access the text file through the following link give you an idea about its structure. text.txt
for vector in f, what gets passed to your code in thevectorvariable is not a vector, but rather one entire line of the file (as a string). You want to do something likefor line in fand write code inside the loop to split up each line into the tokens of interest (using regular-expressions and/orstr.split(), for example) . Then make sure you convert those tokens to numeric values, before using them to fill up your array.