I need to write several NxM arrays generated with numpy to ascii files in the following format: tab separated values, row per line, no brackets, the dimension of the array (columns rows) in the header.
I have tried something like this:
import numpy as np
mat = np.random.random((73,92))
dim = mat.shape
header = "{col}\t{row}\n".format(row = dim[0], col = dim[1])
content = str(mat).replace(' [', '').replace('[', '').replace(']', '').replace(' ', '\t')
mat_PM = open('./output_file.dat', "w ")
mat_PM.write(header)
mat_PM.write(content)
mat_PM.close()
which returns a file with a summarized array, like this:
How do we print the full array?.
Any help is much appreciated.
CodePudding user response:
Numpy has a function to write arrays to text files with lots of formatting options - see the docs for numpy.savetxt
and the user guide intro to saving and loading numpy objects.
In your case, something like the following should work:
with open('./output_file.dat', "w ") as mat_PM:
mat_PM.write(header)
np.savetxt(mat_PM, mat, fmt="