Jase
Jase

Reputation: 51

Reading Matrix from file

I have a txt file consisting some numbers with space and I want to make it as three 4*4 matrixes in python. Each matrix is also divided with two symbols in the text file. The format of the txt file is like this:

1 1
0 0 0 0
0 0 0 0
0 0 0 0 
0 0 0 0
1 1
0 0 0 0
0 0 0 0
0 0 0 0 
0 0 0 0
1 1
0 0 0 0
0 0 0 0 
0 0 0 0
0 0 0 0

My code is now like this but it is not showing the output I want.

file = open('inputs.txt','r')
a=[]
for line in file.readlines():
    a.append( [ int (x) for x in line.split('1 1') ] )

Can you help me with that?

Upvotes: 1

Views: 947

Answers (3)

Zaero Divide
Zaero Divide

Reputation: 799

Assuming your file is not humongous, it is numerical and 4x4, the easiest method is:

  • read all the file
  • split it in blocks with the 1 1\n separator
  • discard the separator items (if block)
  • convert block to vector with split
  • reshape each vector to 4x4
  • make it integer

In a single line: matrixes = [np.reshape(np.array(block.split()),(4,4)).astype(int) for block in open('inputs.txt').read().split('1 1\n') if block]

Caveat: if a matrix reads x x 1 1 in one of its rows, it will be considered a split regardless. Using a value that could be used in the matrix is not a good idea.

That could be prevented splitting on \n1 1\n, and removing by hand the first 4 characters (1 1\n). Also this implementation may be more efficient, flattening everything and then reshaping:

dd = open('inputs.txt').read()[4:]
nmats = dd.count('\n1 1\n') +1
matrixes = np.reshape(np.array(dd.replace('\n1 1\n',' ').split()).astype(int),(nmats,4,4))

This last option returns it as a single 3D matrix:

>>> matrixes
array([[[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]],

       [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]],

       [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]]])
>>> matrixes[0]
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

Upvotes: 1

Necklondon
Necklondon

Reputation: 998

A good old pure python algorithm (assuming matrices can hold string values, otherwise, convert as required):

file = open("inputs.txt",'r')
matrices=[]
m=[]
for line in file:
   if line=="1 1\n": 
      if len(m)>0: matrices.append(m)
      m=[]
   else:
      m.append(line.strip().split(' '))
if len(m)>0: matrices.append(m)
print(matrices)
# [[['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0']], 
#  [['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0']], 
#  [['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0']]]

Upvotes: 1

j1-lee
j1-lee

Reputation: 13939

One option is to use groupby:

from itertools import groupby

matrices = []

with open('inputs.txt', 'r') as f:
    for separator, lines in groupby(f, lambda line: line.strip() == '1 1'):
        if not separator:
            matrices.append([[int(x) for x in line.split()] for line in lines])

print(matrices)
# [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
#  [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
#  [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]

Upvotes: 1

Related Questions