import numpy as np
mat = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
with open('outfile.txt') as f:
    for line in mat:
        np.savetxt(f, line, fmt='%.2f')
How to write a numpy matrix in a text file - python