0

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: output_file.dat

How do we print the full array?.

Any help is much appreciated.

Edson Passos
  • 117
  • 10
cavereaper
  • 41
  • 7

2 Answers2

2

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="%9f", delimiter="\t")
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
1

I would use Python's str.join() method in order to manually create the content string (instead of using str() on the matrix):

content = "\n".join(["\t".join([str(value) for value in row]) for row in mat])

Or the same technique but without using list comprehensions:

row_strings = []
for row in mat:
    values = []
    for value in row:
        values.append(str(value))
    row_strings.append("\t".join(values))
content = "\n".join(row_strings)

If instead you want to use numpy's built in string method, you can configure numpy to return the whole array (as explained in this answer):

np.set_printoptions(threshold=np.inf, linewidth=np.inf)
content = str(mat).replace(' [', '').replace('[', '').replace(']', '').replace(' ', '\t')

Hope this is useful :)

  • 1
    at the risk of being self-promotional because the other answer is mine - I've gotta say that you really shouldn't manually write the csv this way. [`np.savetxt`](https://numpy.org/devdocs/reference/generated/numpy.savetxt.html) is much faster, less error prone, and is designed exactly for this purpose. – Michael Delgado Oct 10 '22 at 17:39