I have seen a few examples over here where it was explained how to print a matrix to a .txt file. However, I haven't found a simple way to perform what I would like to do, which is to print several matrices that are written in a .txt in which additional text is also being printed. Note, this text is not meant to be a headers or something, therefore solutions using dataframe are not really suited.
supposing I have the following matrices:
M1 = np.zeros((6, 6))
M2 = np.zeros((6, 6))
M3 = np.zeros((6, 6))
I would like to print the matrix relative to the line on-top
with open("example.txt", "w") as f:
f.write("------- MATRIX 1 ------------------------------------------------------------\n")
f.write("------- MATRIX 2 ---------------------------------------------------------\n")
f.write("------- MATRIX 3 ---------------------------------------------------------\n")
The results I would be expecting would be a .txt file similar like this:
------- MATRIX 1 ------------------------------------------------------------
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
------- MATRIX 2 ------------------------------------------------------------
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
------- MATRIX 3 ------------------------------------------------------------
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
CodePudding user response:
Using numpy.savetxt
is a proper choice for such case;
specific parameters allow to set format, header and comment:
with open('test.txt', "w") as f:
np.savetxt(f, M1, fmt='%d', delimiter=' ', comments='', header="------- MATRIX 1 ------------------------------------------------------------\n")
np.savetxt(f, M2, fmt='%d', delimiter=' ', comments='', header="------- MATRIX 2 ------------------------------------------------------------\n")
np.savetxt(f, M3, fmt='%d', delimiter=' ', comments='', header="------- MATRIX 3 ------------------------------------------------------------\n")
CodePudding user response:
If you really just want to print only those 3 lines, you can use this. The delimiter is what you can use for the spacing, it is one white space as default, here I used 3 to somewhat match what you were looking for.
For some parameters that you can use for formatting, check here.
import numpy as np
M1 = np.zeros((6, 6))
M2 = np.zeros((6, 6))
M3 = np.zeros((6, 6))
with open("C:/temp/test.txt", "w") as f:
f.write("------- MATRIX 1 ------------------------------------------------------------\n")
np.savetxt(f, M1, fmt='%g', delimiter=' ')
f.write("\n------- MATRIX 2 ------------------------------------------------------------\n")
np.savetxt(f, M2, fmt='%g', delimiter=' ')
f.write("\n------- MATRIX 3 ------------------------------------------------------------\n")
np.savetxt(f, M3, fmt='%g', delimiter=' ')
This code produces the following:
------- MATRIX 1 ------------------------------------------------------------
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
------- MATRIX 2 ------------------------------------------------------------
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
------- MATRIX 3 ------------------------------------------------------------
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
CodePudding user response:
I'm not such a good with numpy
to know builtin workarounds, but you can get this output like that:
M1 = np.zeros((6, 6))
M2 = np.zeros((6, 6))
M3 = np.zeros((6, 6))
with open('example.txt', 'w') as f:
for idx, matrix in enumerate((M1, M2, M3)):
f.write(f'------- MATRIX {idx 1} ------------------------------------------------------------\n')
for matrix_row in matrix:
row =' '.join(str(i) for i in matrix_row) '\n'
f.write(row)
Output:
------- MATRIX 1 ------------------------------------------------------------
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
0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0
------- MATRIX 2 ------------------------------------------------------------
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
0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0
------- MATRIX 3 ------------------------------------------------------------
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
0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0
*If it's important to get 0
instead of 0.0
, it's possible to convert values to int
before converting to str
here:
row =' '.join(str(int(i)) for i in matrix_row) '\n'
OR define value types inside numpy array:
M1 = np.zeros((6, 6), dtype=int)
M2 = np.zeros((6, 6), dtype=int)
M3 = np.zeros((6, 6), dtype=int)
to get:
------- MATRIX 1 ------------------------------------------------------------
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
------- MATRIX 2 ------------------------------------------------------------
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
------- MATRIX 3 ------------------------------------------------------------
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