You have the # lines as a separation between your matrix. So if you loop line by line on your file, you can separate the matrix with this # lines, and built them :
file = open("file.txt", "r") lines = file.readlines()
At this point, lines is a list. lines[i] is the line n° i+1 as a string.
# k, a counter to loop on the lines k = 1 # Matrix_list is a list of the matrix (size i*j) like that [40*1, 36*1, ...] Matrix_list =  while k < len(lines): if "#" in lines[k-1] and "#" not in lines[k]: # Start a new matrix row =  # Loop to get all the lines of the current matrix while "#" not in lines[k]: if k > len(lines): break row.appends(lines[k]) k +=1 # At this point, row is a list of every row of your matrix # First we get the matrix size i*j and create a matrix i = len(row) j = len(row.split()) Mat = np.zeros((i, j)) row_position = 0 for elt in row: colum_position = 0 L = elt.split() for data in L: Mat[row_position, colum_position] = data colum_position += 1 row_position +=1 # Once all the data you had was palced in the matrix : Matrix_list.append(Mat) else: k += 1
Well I hope you get the idea of the algorithm, though i’m pretty sure it won’t work right away. Need to do some test and adjustments, but the global idea should do the trick. At the end you’ll have Matrix_list, a list with every matrix of your txt file as a numpy array.
Once you have that, well you can do whatever you want with each matrix.
solved Read text files long text file in python