Mohannad
Mohannad

Reputation: 113

Creating n*n matrix from text file contains list of lists in python

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

Upvotes: 0

Views: 382

Answers (3)

Ajax1234
Ajax1234

Reputation: 71471

I would do this:

  f = open('first.txt').readlines()
  f = [i.strip('\n') for i in f]

  new_list = []
  final_list = []
  for i in f:
      new_list.append(i.split(' '))

  for i in new_list:
      final_list.append(map(int, i))

  print final_list

we read the contents into f, split them at the spaces and append them to new_list, and then map over each string row in the matrix formed and append that to final_list, which will give you the matrix you would like.

Upvotes: 0

bluprince13
bluprince13

Reputation: 5001

Starting with a string, you need to do the following steps to get a list of numbers from it:

  1. Use strip to get rid of []
  2. Use split(",") to split the string at every comma and create a list of strings
  3. Use map(float, vector) to convert it to a list of floats
  4. Use list() to get an actual list rather than a mapped object

You should then be able to put this into your numpy matrix. It's not necessary to use map(float, vector) if using numpy though, because numpy will infer that they are floats automatically.

Here is a sample code:

myMtrx = np.empty((1137,1137))
f = open("text.txt", "r")

for idx, vector in enumerate(f):
    # vector = '[99.25, 14.74, 26.12, 20.91, 37.14, 79.03, 17.68,  28.4, 0]'
    vector = list(map(float, vector.strip("[]\n").split(",")))
    myMtrx[idx ,:] = vector

Upvotes: 0

stellasia
stellasia

Reputation: 5622

You are reading string from your file, that you need to convert to list. A solution like this one may do the trick:

for line in f:
   vector = line.strip("[]").split(",")
   ...
  • strip : remove all characters in "[]" from begining and end of string
  • split: transform string to list, cutting at each "," position

Upvotes: 1

Related Questions