Reading Matrix from file
Asked Answered
H

3

1

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?

Hyphenated answered 15/5, 2022 at 20:29 Comment(5)
What is the meaning of lines with 1 1, do they separate matrixes?Duello
The format of the output needs to be a three 4*4 array(matrixes) using numpy. 1 1 is the separation of the matrixesHyphenated
you should rather read all - read() and later split it with split('1 1') to create 4 strings with matrixes. And later you would have to split these strings using "\n" and space.Granophyre
maybe first ise print() to see what you get in line and line.split('1 1') and x (before you use int(). It is called "print debuging" and it helps to see what code is doing.Granophyre
Storing the whole file into memory is not recommended. It might be terabytes...Jerad
J
1

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']]]
Jerad answered 15/5, 2022 at 20:40 Comment(2)
How can I make it three 4*4 matrixes? For example writing the first row in a 4*4 matrix? I want them to be shown not right to each other but like 4 different lines under each otherHyphenated
There are no such things as matrixes in python. Only lists. You might want to look at numpy for examples, but I don't know exactly what you need. BTW screen output is independent of storage in memory.Jerad
R
1

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]]]
Reef answered 15/5, 2022 at 20:37 Comment(1)
How can I make it three 4*4 matrixes? For example writing the first row in a 4*4 matrix? I want them to be shown not right to each other but like 4 different lines under each otherHyphenated
J
1

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']]]
Jerad answered 15/5, 2022 at 20:40 Comment(2)
How can I make it three 4*4 matrixes? For example writing the first row in a 4*4 matrix? I want them to be shown not right to each other but like 4 different lines under each otherHyphenated
There are no such things as matrixes in python. Only lists. You might want to look at numpy for examples, but I don't know exactly what you need. BTW screen output is independent of storage in memory.Jerad
S
1

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]])
Sonata answered 15/5, 2022 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.